• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Vector vs. ArrayList

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused over the performance of both. I have this servlet to generate stock charts which runs on Sun 5.9. The original servlet was written using Vectors, there were a lot of them created, updated, and removed. The CPU usage (according to top) was around 3.50% all the time. Then I've converted Vectors to ArrayLists because there's no need for synchronization. I expected to have CPU go down, but instead it jumped to 7%. Then I found another two Vectors, converted them to ArrayList and CPU reduced itself to 5%. Although I have to mention that servlets now respond somewhat faster.

Can someone point out the reason behind all this?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A wild guess: The CPU usage went up because it wasn't waiting on the syncronization? Not sure how syncronization actually affects CPU usage.

With the net change being from 3.5% to 5% and the app appearing to respond better I probably wouldn't worry about it.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems unlikely that switching a few Vectors to ArrayLists would cause a change in CPU usage -- unless they were used read-only by many threads (servlets) at once.

Note that while a thread is waiting to acquire a synchronization lock, it isn't using any CPU cycles. So an app with a lot of contention will spend a lot of time doing nothing. By removing the contention, you free up the threads to do work, resulting in higher CPU utilization.

This would explain higher percentage in top combined with better responsiveness.
 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes perfect sense. Thank you guys!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Harkness:

Note that while a thread is waiting to acquire a synchronization lock, it isn't using any CPU cycles. So an app with a lot of contention will spend a lot of time doing nothing. By removing the contention, you free up the threads to do work, resulting in higher CPU utilization.



True - but if synchronization makes this difference, wouldn't that be a strong hint to the fact that synchronization actually *might* be needed?
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you check how much time the CPU was spending doing context switches between threads in your old version of the code vs new version of the code?

Over-Sychronization of your code might result in higher number of context switches, which is bad for performance. In Windows, the OS doesnt report the time spent in context switching as CPU usage of the process (I don't know about Sun). So, if you have 10 threads running, with each of them taking 5% CPU, the OS will report the CPU usage as 50%. But, the Kernel might have used 5% of the CPU switching between your threads, which won't be reported. So, your process is contributing to 55% of CPU usage, and not 50%. Usually, context switching is not a major factor, unless you have a huge number of threads that are frequently going into wait state.

No answer for you, but just a reminder that to accurately compare performance you have to take Context Switches/sec and Kernel time into account. You might find that the Kernel might be using less CPU in your optimized code.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
True - but if synchronization makes this difference, wouldn't that be a strong hint to the fact that synchronization actually *might* be needed?

Yes, I'd say that's a reasonable first cut. However, that's why I stated read-only use of ArrayList. If you had contentious read-write usage, you'd better synchronize!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Harkness:
Yes, I'd say that's a reasonable first cut. However, that's why I stated read-only use of ArrayList. If you had contentious read-write usage, you'd better synchronize!



Ah, I see...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic