Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 2771 times)
0 Members and 1 Guest are viewing this topic.
Offline NativeOne

Senior Newbie

Java games rock!

« Posted 2004-01-20 21: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

Exp: 12 years

Where's the Kaboom?

« Reply #1 - Posted 2004-01-20 21: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 22: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!
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 22: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  

EgonOlsen (45 views)
2018-06-10 19:43:48

EgonOlsen (27 views)
2018-06-10 19:43:44

EgonOlsen (47 views)
2018-06-10 19:43:20

DesertCoockie (210 views)
2018-05-13 18:23:11

nelsongames (127 views)
2018-04-24 18:15:36

nelsongames (126 views)
2018-04-24 18:14:32

ivj94 (867 views)
2018-03-24 14:47:39

ivj94 (128 views)
2018-03-24 14:46:31

ivj94 (779 views)
2018-03-24 14:43:53

Solater (143 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!