• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Transfer rate and Java

 
Kumar Kausikasa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to calculate the rate/speed bytes are placed on the wire. This is what i have done
byte[] bytes = new byte[1024*32];
ByteArrayOutputStream byteout = new ByteArrayOutputStream(1024*100);
ObjectOutputStream objectout = new ObjectOutputStream(byteout);
long startbyte = System.currentTimeMillis();
objectout.write(bytes);
long endbyte = System.currentTimeMillis();
Is this the right way ?.
I have also found that it is not linear.
I have observed this
1024 * 32 bytes less than 1 millisec
1024 * 100 bytes 4~7 millisec
1024 * 128 bytes less than 1 millisec
1024 * 1024 bytes 134 millisec
Does someone through more light on this.
Thanks for your time.
Thanks
Praveen.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to the Performance forum...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not unusual for performance times to vary like this. A primary cause is the way the JIT compier works at runtime. The first (few) time(s) a given piece of code runs, the JIT has not had time or reason to optimize the code yet, so it runs slowly. Then after the code has run several times the JIT decides it's worthwhile to optimize it - and while optimizing, it may run even slower as the JIT is using JVM cycles to do its work. But when the optimization is done, the code runs much more quickly, and will (probably) continue to do so each time you re-run it, until the JVM exits. So, many things get faster when you repeat them. To get meaningful performance measurements, repeat the code you want to test several times, until the performance times settle down to a nice repeatable average. (Just ignore the initial numbers for most applications.)
Additionally, performance of ByteArrayOutputStream will vary depending on how many bytes you write. A BAOS maintains an internal array of the bytes that have been written so far. When you write more bytes than it has space for, there is a short delay while the BAOS creates a new array twice as big as the old one, copies the old data into the new array, and continues writing new data to the new array (discarding the old one). This creates delays of variable length at unpredicatble times. (Well, they're predicatble if you pay attention to how many bytes have been written.) A BAOS start with an array of size 32 - so the first 32 bytes can be written quickly, then there's a short delay while resizing to 64, then you can write 32 more bytes for a total of 64 before there's another longer delay as you resize to 128, then write 64 more bytes quickly before there's an even longer delay while resizing to 256... and so on. The more you write, the let frequently the delays occur, but the bigger they are when they do occur. You can often bypass this by presizing the array to be bigger than you need brom the very beginning. If you know you're going to write 4000-8000 bytes, create new ByteArrayOutputStream(8000) and you won't have to worry about resizine effects (unless you write more then 8000 bytes).
Note - ArrayList and Vector behave very similarly in ths regard. Probably other classes too which I'm forgetting. HashMap, Hashtable, HashSet too I think (though it's more complicated). It's not unusual for any structure that can store a variable number of elements. Basically, performance of these things may be linear on average, but the graph will have a lot of ups and downs depending how many elements you have. Usually it's not worth worrying about much - just accept that performance is often not linear.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the measured times are in the 0-10 millisecond range, you can only take them so seriously. Hundrews of random factors on your computer could affect the time. If you're going to run a microbenchmark, the numbers won't mean anything unless you've repeat the test enough times to add up to something measurable like a second or two.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!