cast double array to float

P

Philipp

Hello
Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?

Thanks for your answers.
Phil

I now have this (doublePic is of type double[][])

float[][] floatPic = new float[height][width];
for(int j=0; j<height; j++){
for(int i=0; i< width; i++){
floatPic[j] = (float)doublePic[j];
}
}
 
R

Robert Klemme

Hello
Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?

Thanks for your answers.
Phil

I now have this (doublePic is of type double[][])

float[][] floatPic = new float[height][width];
for(int j=0; j<height; j++){
for(int i=0; i< width; i++){
floatPic[j] = (float)doublePic[j];
}
}


This is not a cast but a conversion. The only thing that could be
improved is to use ".length" in your expressions instead of variables
"height" and "width".

Kind regards

robert
 
C

Chris Uppal

Philipp said:
Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?

No.

You may be able to find a pre-packed routing to do it for you (I don't /think/
any exist, but I could easily be wrong); but it'll just be doing loops inside,
the same as if you did it yourself.

-- chris
 
P

Philipp

Chris said:
Philipp wrote:

Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?


No.

You may be able to find a pre-packed routing to do it for you (I don't /think/
any exist, but I could easily be wrong); but it'll just be doing loops inside,
the same as if you did it yourself.

I was thinking of the likes as System.arraycopy() which is a native
method (as far as I can tell) so is maybe faster (?)...

But it needs to convert from double to float while copying...

Phil
 
D

Daniel Pitts

Philipp said:
Chris said:
Philipp wrote:

Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?


No.

You may be able to find a pre-packed routing to do it for you (I don't /think/
any exist, but I could easily be wrong); but it'll just be doing loops inside,
the same as if you did it yourself.

I was thinking of the likes as System.arraycopy() which is a native
method (as far as I can tell) so is maybe faster (?)...

But it needs to convert from double to float while copying...

Phil

Try arraycopy out, I don't know if it'll work, but it might convert for
you.
 
P

Patricia Shanahan

Daniel said:
Philipp said:
Chris said:
Philipp wrote:


Is there a nicer (or faster) way to cast a double[][] to a float[][]
than doing loops?

No.

You may be able to find a pre-packed routing to do it for you (I don't /think/
any exist, but I could easily be wrong); but it'll just be doing loops inside,
the same as if you did it yourself.
I was thinking of the likes as System.arraycopy() which is a native
method (as far as I can tell) so is maybe faster (?)...

But it needs to convert from double to float while copying...

Phil

Try arraycopy out, I don't know if it'll work, but it might convert for
you.

One of its conditions for throwing an ArrayStoreException is "The src
argument and dest argument refer to arrays whose component types are
different primitive types."

Patricia
 
C

Chris Uppal

Philipp said:
I was thinking of the likes as System.arraycopy() which is a native
method (as far as I can tell) so is maybe faster (?)...

<reminisce>I think it was when 1.3 came out, with the first release of the
Hotspot JIT, that the rumour went around that System.arraycopy() was now
implemented in Java, since that was as fast as a native version would have
been. I never did bother to check (I dunno why).</reminisce>

Whatever, I don't think that's true anymore. The 1.5 Sun JMV generates machine
code implementations for the various combinations of types and overlapping vs.
non-overlapping copies as part of its startup sequence, and (I presume) patches
them directly into the implementations of System.arraycopy(). Hard to imagine
Sun's engineers bothering with that unless they knew they could squeeze useful
extra speed that way (I must measure it someday...).

But it needs to convert from double to float while copying...

Yeah. I suspect that's the problem. If there's no hyper-efficient, low-level,
bit-for-bit copy instruction(s) available, then there's not a lot to gain by
having a pre-packaged "wrapper" for low-level code.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top