您的位置:首页 > 其它

NSTimeTnterval 用法系统排序,冒泡排序,选择排序比较

2014-08-18 20:45 369 查看
//
性能比较: 系统排序速度最快,
冒泡排序和选择排序时间差不多, 但同样比系统慢很多很多

//
排序次数少不太明显, 次数多就显现出来了

//NSLog(@"��������������
系统排序
��������������");
NSTimeInterval aTimer =
0;

for (int i =
0; i < 10000; i++) {
NSDate *date1 = [NSDate date];
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"v",
@"f", @"a",
@"c", @"w",
nil];
[array sortedArrayUsingSelector:@selector(compare:)];

NSDate *date2 = [NSDate date];
aTimer = [date2 timeIntervalSinceDate:date1] *
1000; // 毫秒数要乘以1000, 本身得到的是秒数
}

NSLog(@"♻️♻️♻️系统排序所用时间:毫秒数: %f",
aTimer);

//NSLog(@"��������������
冒泡排序
��������������");
NSTimeInterval aTimer2 =
0;

for (int i =
0; i < 10000; i++) {
NSDate *date3 = [NSDate date];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"v",
@"f", @"a",
@"c", @"w",
nil];

int count = (int)[array2 count];

for (int i =
0; i < count - 1; i++) {

for (int j =
0; j < count - 1 - i; j++) {

if ([array2[j] compare:array2[j +
1]] > 0) {
[array2 exchangeObjectAtIndex:j withObjectAtIndex:j +
1];
}
}
}

NSDate *date4 = [NSDate date];
aTimer2 = [date4 timeIntervalSinceDate:date3] *
1000;
}
NSLog(@"♻️♻️♻️冒泡排序所用时间:毫秒数:
%f", aTimer2);

//NSLog(@"��������������
选择排序
��������������");
NSTimeInterval aTimer3 =
0;

for (int i =
0; i < 10000; i++) {
NSDate *date5 = [NSDate date];
NSMutableArray *array3 = [NSMutableArray arrayWithObjects:@"v",
@"f", @"a",
@"c", @"w",
nil];

int count3 = (int)[array3 count], min =
0;

for (int i =
0; i < count3 - 1; i++) {
min = i;

for (int j =
0; j < count3 - 1 - i; j++) {

if ([array3[j] compare:array3[j +
1]] > 0) {
min = j;
}

if (min != i) {
[array3 exchangeObjectAtIndex:i withObjectAtIndex:min];
}
}
}
NSDate *date6 = [NSDate date];
aTimer3 = [date6 timeIntervalSinceDate:date5] *
1000;
}
NSLog(@"♻️♻️♻️选择排序所用时间:毫秒数:
%f", aTimer3);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: