Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Copying a 2 Dim Array into a 1 dim array  (Read 1055 times)
0 Members and 2 Guests are viewing this topic.
Offline Mojomonkey

JGO Ninja
***

Posts: 540
Medals: 3


ooh ooh eee eeee


« on: 2003-07-09 15:27:34 »

Is there any speedier ways to copy a uniform (i.e. square) two dimesional array into a single dimesional array?

Something faster than:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
float[][] source = new float[size][size];
float[] target = new float[size * size];

// .... source is filled

for(int i = size; i < size; i++) {
    for(int j = size; j < size; j++) {
        target[j*size + i] = source[i][j];
    }
}

Don't send a man to do a monkey's work.
Offline nickdotjava

Full Member
**

Posts: 149


I have fallen to the dark side. &nbsp;I'm using DX9


« Reply #1 on: 2003-07-09 15:41:37 »

As far as I know, that's the way to do it.

And is that the actual code, or something quickly written for the post?  There are a few bugs (loops never entered)...

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline Mojomonkey

JGO Ninja
***

Posts: 540
Medals: 3


ooh ooh eee eeee


« Reply #2 on: 2003-07-09 15:45:29 »

just wrote something up real quick for the post.

Don't send a man to do a monkey's work.
Games published by our own members! Go get 'em!
Offline mik

JGO n00b
*

Posts: 31


Java games rock everywhere!


« Reply #3 on: 2003-07-09 16:41:40 »

and a System.arraycopy instead of the second loop?
like :
for (int i = 0; i < size; i++)
{
  System.arraycopy(source, 0, target, i*size, size);
}

as the source and the target have same type

Duh

Mik
Offline tortoise

Full Member
**

Posts: 230


<3 Shmups


« Reply #4 on: 2003-07-11 20:49:15 »

Sun would claim arraycopy is faster. I don't remember the details on that method but it uses some native trickery to boost its speed.
Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8076
Medals: 91


Eh? Who? What? ... Me?


« Reply #5 on: 2003-07-12 01:07:19 »

Arraycopy only has to do one bounds check.
The server VM should eliminate the bounds checks and also remove the native call overhead but it's probably neck and neck. But because almost no-one has the server VM (grrr!) you'll be better off with the arraycopy method I suspect.

Cas Smiley

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.078 seconds with 19 queries.