Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Array access pretty slow  (Read 1518 times)
0 Members and 1 Guest are viewing this topic.
Offline NativeOne

Senior Newbie




Java games rock!


« Posted 2004-01-20 22:45:31 »

Lastly I tried an optimization in a frequently executed piece of code: 3 variables of small size needed to be saved in a sort of "stack array" of fixed size. In the first version each variable was stored and retrieved from an individual array.

After packing all info into one single array with shifting and oring (saving two array acceses), the code was significantly faster! Obviously array access is much more expensive than a few bit-ops...

But how expensive is it really? I would guess it takes at least two condition checks for the bounds and one multiplication to calculate the address. Does anyone know exactly?


Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2004-01-20 22:53:59 »

It doesn't likely take a multiply to calculate the address.. more likely a shift or perhaps even a native addressing mode of the processor,  since the sizes of array entries are always powers of two.

byte = 1
char/short = 2
int/float/obj. ref. = 4
long/double/(obj.ref. 64-bit VM) = 8

You are timing both bounds checking AND memory bandwidth.  In your packed version there is a single memory access and the shifts and stuff will happen in registers.

Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2004-01-20 23:13:48 »

You say its a stack.

java Arrays are a very inefficient way to do stacks.  if this can be coded recursive where the stack is implied on the system stack youll get much better performance.

The reason is bounds-checking.  The Vm doesnt knwo what you have is a stack so it treats every access as dangerous.  The system stack is implemented with that knowledge and cna be much more efficient. (In the Hotspot case it just puts a memory trap right above and right below the stack.)

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline NativeOne

Senior Newbie




Java games rock!


« Reply #3 - Posted 2004-01-20 23:36:32 »

Oh, that was a lighning fast reply Shocked

No, unfortunately i can't (or at least it would be very messy) implement the stack with recursion.

Another question: generally what is faster, 2 accesses to arrays with length 64 or 1 access to an array with 4096 length? I suppose this depends on CPU caching... so it's platform dependant... a rule of thumb?

Have a nice day!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (93 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!