convert NSArray in float[] - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Graphics & Audio Programming (/forum-9.html) +--- Thread: convert NSArray in float[] (/thread-3227.html) convert NSArray in float[] - bonanza - Jun 18, 2007 05:12 AM Hi, anyone know, how convert an NSArray to float[]? NSArray *_vertices; //my Vertex Data -> 1.0, 1.0, 1.0 tell OpenGL to use my NSArray but how?? glVertex3fv(); convert NSArray in float[] - ThemsAllTook - Jun 18, 2007 05:57 AM Here's one (probably not particularly fast) way to do it: Code: ```int count, index; float * floatArray; count = [myNSArray count]; floatArray = (float *) malloc(sizeof(float) * count); for (index = 0; index < count; index++) {   floatArray[index] = [[myNSArray objectAtIndex: index] floatValue]; }``` (untested code, but should work) If this is a list of vertices you'll be passing to OpenGL, you might not want to put it into an NSArray in the first place if you can avoid it... convert NSArray in float[] - Cochrane - Jun 18, 2007 07:18 AM Using an NSEnumerator might be slightly faster: Code: ```NSEnumerator *enumerator; float * floatArray; id floatObject; floatArray = (float *) malloc(sizeof(float) * [myNSArray count]); enumerator = [myNSArray objectEnumerator]; while(floatObject = [enumerator nextObject]) {   floatArray[index] = [floatObject floatValue]; }``` (also untested) But of course, this is still a slooooooow thing to do, so follow ThemsAllTook's advice and don't use an NSArray for this if possible. convert NSArray in float[] - unknown - Jun 18, 2007 08:02 AM Instead of not doing it at all, its probably acceptable to just do it once (instead of once per frame). convert NSArray in float[] - bonanza - Jun 18, 2007 08:55 AM ThemsAllTook Wrote:Here's one (probably not particularly fast) way to do it: Code: ```int count, index; float * floatArray; count = [myNSArray count]; floatArray = (float *) malloc(sizeof(float) * count); for (index = 0; index < count; index++) {   floatArray[index] = [[myNSArray objectAtIndex: index] floatValue]; }``` (untested code, but should work) If this is a list of vertices you'll be passing to OpenGL, you might not want to put it into an NSArray in the first place if you can avoid it... thanks, works well convert NSArray in float[] - AnotherJake - Jun 18, 2007 08:55 AM Cochrane Wrote:Using an NSEnumerator might be slightly faster: That's what I keep thinking too, but I haven't found any noticeable difference yet anywhere I've tested for it. convert NSArray in float[] - TomorrowPlusX - Jun 25, 2007 10:33 AM You will notice it, eventually! A few years ago I was profiling some shadow volume extrusion code and it turned out that using std::vector's array operator was killing performance. Casting my array's front() to a raw pointer, and accessing that brought CPU hit in the internal loops from something like 80% of CPU time down to essentially nothing. I'm not suggesting premature optimization, but keep in mind to shark your code now and then. You might write a correct but slow path and forget all about it until, 6 months from now when you're really stress testing your code, you wonder "Why is this so slow?" What I'd do, personally, is let NSArray manage your storage while you're building or otherwise manipulating your data. But once your data's ready, copy it over to a raw array and let go of the NSArray. Or, if you're a C++ guy like me, just use std::vector, std::set, etc. They're surprisingly fast (enough).