排序与Objective-C的一个int数组 - 优化 [英] Sorting a array of int with Objective-C - Optimizing

查看:152
本文介绍了排序与Objective-C的一个int数组 - 优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天上午,我偶然在此线程<一个href=\"http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array\">Why正在处理一个有序数组的速度比未排序数组?并发现它真的很有趣!

This morning, I stumbled on this thread Why is processing a sorted array faster than an unsorted array? and found it really interesting !

我想给它Objective-C的一个尝试,而实现它,我面对的排序整数数组的问题。因此,以下问题。

I wanted to give it a try in Objective-C and while implementing it, I faced the problem of sorting an array of integers. Hence the following question.

让我们考虑 ARRAYSIZE 整数数组,用0和256之间的随机值初始化的:

Let's consider an array of arraySize integers, initialized with random values between 0 and 256:

int data[arraySize];
for (int c = 0; c < arraySize; ++c)
{
    data[c] = arc4random() % 256;
}

我想这个排序阵列和结果存储在另一个整数数组。在C ++中,我们可以做这样的事情:

I would like to sort this array and store the result in another array of integers. In C++ we could do something like :

std::sort(data, ...);

在Java中,我们将使用:

In Java, we would use :

Arrays.sort(data);

在Objective-C中,我已经做了这样的:

In Objective-C, I've done it like that :

int sortedData[arraySize];
NSArray* sortedArray = [NSArray array];
// Initialize the array to sort.
for ( int i = 0 ; i < arraySize ; ++i )
{
    sortedArray = [sortedArray arrayByAddingObject:[NSNumber numberWithInt:data[i]]];
}
// Sort the array.
sortedArray = [sortedArray sortedArrayUsingSelector:@selector(compare:)];
// Copy the array back into a int[] array.
for (int c = 0; c < arraySize; ++c)
{
    sortedData[c] = [sortedArray[c] intValue];
}

它的工作原理,但在我看来,这是一个真正的痛苦,它不是在所有的优化!我怎么能提高呢?

It works, but it seems to me it's a real pain and it's not optimized at all ! How could I improve this ?

推荐答案

非优化的说法是真实的只为code你有的。苹果公司的框架是高度优化,你不应该来揣摩苹果,他们已经猜到了第二个你。

The "non-optimized" statement is true only for the code you have. Apple's frameworks are highly optimized and you're not supposed to second guess Apple, they've already second guessed you.

首先,使用它们已被用于创建的目的的方法。您正在创建的无序排列的方式会浪费内存。在循环的每一个步骤,你创建阵列的新实例,到最后,你会用256结束(或任何原整数数组的计数)阵列,这就是只是画蛇添足

First, use methods for the purpose they have been created for. The way you're creating the unsorted array just wastes memory. In every single step of the loop, you're creating a new instance of the array, in the end, you'll end up with 256 (or whatever the count of the original integer array is) arrays, that's just superfluous.

所以,如果你的真的不好想使用Objective-C的解决这个问题,你可以使用一个可变的数组,你只需要一个的NSMutableArray

So, if you really, badly want to use Objective-C for solving this problem, you can use a mutable array and you will only need one NSMutableArray:

int array[256];
// populate the C array here

NSMutableArray *objcArray = [NSMutableArray array];
for (int i = 0; i < sizeof(array) / sizeof(*array); i++) {
    [objcArray addObject:[NSNumber numberWithInt:array[i]];
}

[objcArray sortUsingSelector:@selector(compare:)];

顺便说一下,即使在两个环路(一个用于填充C数组和一个用于将其转换为的NSMutableArray )是不必要的在这里。你可以只写

By the way, even the two loops (one for populating the C array and one for transforming it into an NSMutableArray) are unnecessary here. You could just write

const size_t size = 256;
NSMutableArray *objcArray = [NSMutableArray array];
for (int i = 0; i < size; i++) {
    [objcArray addObject:[NSNumber numberWithInt:arc4random_uniform(256)];
}

[objcArray sortUsingSelector:@selector(compare:)];

不过,你并不真的需要的Objective-C进行排序的整型数组。你可以简单地写一个C函数(或者,如果你想,一个Objective-C法)进行排序就地整数数组,并能在某种程度上更高效的或更快:

However, you don't really need Objective-C for sorting an integer array. You can simply write a C function (or if you want, an Objective-C method) for sorting the integer array in-place, and that can be somewhat more efficient or faster:

#include <math.h>
#include <stdlib.h>
#include <unistd.h>

int compare(const void *first, const void *second)
{
    return *(const int *)first - *(const int *)second;
}

- (void)sortArray:(int *)array ofSize:(size_t)sz
{
    qsort(array, sz, sizeof(*array), compare);
}

然后使用它是这样的:

Then use it like this:

int array[256];
for (int i = 0; i < sizeof(array) / sizeof(*array); i++) {
    array[i] = arc4random_uniform(256);
}

[self sortArray:array ofSize:sizeof(array) / sizeof(*array)];

此外,阅读有关数组的。真正的好文章。

这篇关于排序与Objective-C的一个int数组 - 优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆