• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java memset()

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I know there is no memset() in Java, nor will there ever be.

I've got an 800x600 array I need to clear fairly often. The array is currently boolean, but it could be int. So by 'clear' I mean either 'set each entry to false', or 'set each entry to 0', doesn't matter to me.

What is the most efficient way to clear this array of either booleans or ints (or bytes, I suppose)?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:Yeah, I know there is no memset() in Java, nor will there ever be.

I've got an 800x600 array I need to clear fairly often. The array is currently boolean, but it could be int. So by 'clear' I mean either 'set each entry to false', or 'set each entry to 0', doesn't matter to me.

What is the most efficient way to clear this array of either booleans or ints (or bytes, I suppose)?



The Arrays class has a fill() method that could work. It is, however, written for a single dimension array, so you will need to use a loop.

Henry
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's not fast enough, you could make a small pool of arrays, with a dirtyArrayQueue and a cleanArrayQueue, and have a worker thread (or pool of threads) that takes arrays from the dirty queue, clears them, and puts them in the clean queue. Then any other thread that needs a clean array just puts its current array in the dirty queue, and gets a clean one from the clean queue. This could work well if you have spare cycles at other times to get the arrays cleaned, but need to be able to switch to a clean state very quickly when the need comes up.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like it saves approx. 10 ms no matter the array size. Running on a quad core with 8gig memory. As my array is 800x600 Array.fill() is 6 times faster, well worth the change.


For grins and giggles I also timed ints. Array.fill() now takes 5 ms less for smaller arrays, but the total run time for a 20,000x20,000 array Array.fill() is 5 ms slower. The total time goes up to 368 ms., triple the time for a binary array.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:Looks like it saves approx. 10 ms no matter the array size. Running on a quad core with 8gig memory. As my array is 800x600 Array.fill() is 6 times faster, well worth the change.



Before drawing that conclusion, I would consider running the tests again, but this time, flip the order of the tests. The difference that you are seeing could just be caused by the just-in-time compiler (assuming that you are using -server).

Henry
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How right you are. I just duplicated the double loop test to re-run after the Array.fill(), the second double loop runs even faster than the Array.fill().

I'm not in the mood to wrap my tests in loops of loops at the moment, maybe later I'll look into it more.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:How right you are. I just duplicated the double loop test to re-run after the Array.fill(), the second double loop runs even faster than the Array.fill().

I'm not in the mood to wrap my tests in loops of loops at the moment, maybe later I'll look into it more.



The other possibility, that could be affecting the results, is the garbage collector.... so, yeah, performance testing with Java isn't easy to get done right.

Henry
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think using a BitSet will be faster. Though to access a particular bit you need math, not sure if it would be slower overall (ie sacrifice access speed versus clear speeds may be shooting yourself in the foot...)
results:
Bitset Took: 59ms
Bitset Took: 48ms
Array Took: 450ms
Bitset Took: 42ms
Bitset Took: 59ms
Array Took: 459ms
Array Took: 434ms
Array Took: 448ms
Bitset Took: 58ms
Bitset Took: 52ms


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that unless you have specific, defined parameters on what the performance needs to be, you are wasting your time. How do you know what is 'fast enough', since things could ALWAYS be faster? In six months, or a year, will all this tweaking make sense, or will you have to spend a few extra hours debugging, trying to understand what you did?

If you save 1/3 of a second, you have to run this code an AWFUL lot of times to make up those hours...And that is only if there is actual productivity lost while the user waits that extra fraction of a second...
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's Conway's Life, faster is always better
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:It's Conway's Life, faster is always better


So how much are you spending on a bigger/faster CPU? These days, replacing even one year old hardware can probably make a bigger difference than any micro-optimization.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Venolia wrote:It's Conway's Life, faster is always better



I don't think so. If you get it running so fast that the entire sequence of (let's say) 450 different positions is displayed in under a second, that isn't going to be very interesting. You would want to slow it down so you could see what was happening.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic