从 2D NSArray 创建 C 数组
我有一个 2D NSArray 字符串数字,我想将其转换为 2D C 双精度数组,以便与 BLAS/LAPACK 函数一起使用(通过加速框架)。 这行代码似乎可以工作,但是效率似乎非常低,并且最终由于 malloc 错误而崩溃。有没有更有效的方法将此 2D NSArray 转换为 C 数组?或者将 NSArrays 与 BLAS/LAPACK 一起使用的便捷方法?
double gridDataC[[nrows intValue]+1][[ncol intValue]+1];
for(i=6;i<[fileLines count]-1;i++){
for(j=0;j<[ncol intValue]-1;j++){
gridDataC[i][j]=[[[[fileLines objectAtIndex:i] componentsSeparatedByString:@" "] objectAtIndex:j] doubleValue];
}
}
fileLines 是一个数组,其中包含被解析为相应数字的文件行。
I have a 2D NSArray of string numbers that I would like to convert to a 2D C array of doubles for use with BLAS/LAPACK functions (through the accelerate framework).
This line of code seems to work, however seems to be incredibly inefficient and eventually crashes due to a malloc error. Is there a more efficient way to convert this 2D NSArray to a C array? Or a convienent way of using NSArrays with BLAS/LAPACK?
double gridDataC[[nrows intValue]+1][[ncol intValue]+1];
for(i=6;i<[fileLines count]-1;i++){
for(j=0;j<[ncol intValue]-1;j++){
gridDataC[i][j]=[[[[fileLines objectAtIndex:i] componentsSeparatedByString:@" "] objectAtIndex:j] doubleValue];
}
}
fileLines is an array that contains lines of a file that are parsed into respective numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里涉及内存的东西很少。
1.
componentsSeparatedByString:
创建一个自动释放的数组。由于您正在循环查找该字符串中的每个对象,因此您将多次创建类似的数组。由于自动释放的对象直到运行循环结束才被释放,这可能会堵塞内存。最好通过将方法调用移出内循环来执行此操作一次。2.
i
的值是最容易混淆的。您将i
作为gridDataC
的索引传递。如果您从i = 6
开始,它可能应该是i - 6
。There are few things here that deal with memory.
1.
componentsSeparatedByString:
creates an autoreleased array. Since you're looping for every object within that string, you are creating similar array multiple times. As the autoreleased objects are not released until the end of the runloop this might clog the memory. It's better to do this once by bringing the method call out of the inner loop.2.The value of
i
is the most confusing. You passi
as the index forgridDataC
. It should probably bei - 6
if you're starting fromi = 6
.