ocfile.h
#import <Foundation/Foundation.h>
@interface ocfile NSObject
{
NSMutableArray *m_pArray;
NSDate *time;
}
@end
ocfile.m
@implementation ocfile
- (instancetype)init
{
self = [super init];
[self test];
return self;
}
- (void)getTimeNow
{
NSDate *pTemp = [[NSDate alloc] init];
NSTimeInterval spec = [pTemp timeIntervalSinceDate:time];
NSLog(@"spec %f", spec*1000);
time = pTemp;
}
-(void)test
{
time = [[NSDate alloc] init];
m_pArray = [[NSMutableArray alloc] init];
[self getTimeNow];
NSLog(@"insert 500 ......\r\n");
int nCount = 0;
for(int n = 0; n < 500; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
NSNumber *num = [[NSNumber alloc] initWithInt:value];
[m_pArray insertObject:num atIndex:value];
nCount++;
}
NSLog(@"insert 500 m_pArray nCount %d", [m_pArray count]);
nCount = 0;
[self getTimeNow];
NSLog(@"sort 500 ......");
[m_pArray sortedArrayUsingSelector:@selector(compare:)];
[self getTimeNow];
NSLog(@"delete 500 ......");
for(int n = 0; n < 500; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
[m_pArray removeObjectAtIndex:value];
nCount++;
}
//NSLog(@"delete 500 nCount %d", nCount);
nCount = 0;
[self getTimeNow];
m_pArray = [[NSMutableArray alloc] init];
NSLog(@"insert 2000 ......\r\n");
for(int n = 0; n < 2000; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
NSNumber *num = [[NSNumber alloc] initWithInt:value];
[m_pArray insertObject:num atIndex:value];
nCount++;
}
NSLog(@"insert 2000 m_pArray nCount %d", [m_pArray count]);
nCount = 0;
[self getTimeNow];
NSLog(@"sort 2000 ......");
[m_pArray sortedArrayUsingSelector:@selector(compare:)];
[self getTimeNow];
NSLog(@"delete 2000 ......");
for(int n = 0; n < 2000; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
[m_pArray removeObjectAtIndex:value];
nCount++;
}
[self getTimeNow];
//NSLog(@"delete 2000 nCount %d", nCount);
nCount = 0;
m_pArray = [[NSMutableArray alloc] init];
NSLog(@"insert 5000 ......\r\n");
for(int n = 0; n < 5000; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
NSNumber *num = [[NSNumber alloc] initWithInt:value];
[m_pArray insertObject:num atIndex:value];
nCount++;
}
NSLog(@"insert 5000 m_pArray nCount %d", [m_pArray count]);
nCount = 0;
[self getTimeNow];
NSLog(@"sort 5000 ......");
[m_pArray sortedArrayUsingSelector:@selector(compare:)];
[self getTimeNow];
NSLog(@"delete 5000 ......");
for(int n = 0; n < 5000; n++)
{
int value = 0;
if ([m_pArray count] > 1)
{
value = arc4random() % ([m_pArray count] - 1);
}
else
{
value = 0;
}
[m_pArray removeObjectAtIndex:value];
nCount++;
}
[self getTimeNow];
//NSLog(@"delete 5000 nCount %d", nCount);
nCount = 0;
NSLog(@"end ......");
}
@end
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var sortInts = UInt32[]();
var someInts = UInt32[]();
var time:NSDate = NSDate();
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
test();
return true
}
func getTimeNow()
{
var temp:NSDate = NSDate();
var spec:NSTimeInterval = temp.timeIntervalSinceDate(time);
println(spec*1000);
time = temp;
}
func test()
{
getTimeNow();
println("insert 500 ......");
for indexOne in 1...500
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.insert(value, atIndex: index);
}
println(someInts.count)
getTimeNow();
println("sort 500 ......");
sort(someInts, {$0 > $1});
getTimeNow();
println("delete 500 ......");
for indexOne in 1...500
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.removeAtIndex(index);
}
someInts = UInt32[]();
getTimeNow();
println("insert 2000 ......");
for indexOne in 1...2000
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.insert(value, atIndex: index);
}
println(someInts.count)
getTimeNow();
println("sort 2000 ......");
sort(someInts, {$0 > $1});
getTimeNow();
println("delete 2000 ......");
for indexOne in 1...2000
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.removeAtIndex(index);
}
someInts = UInt32[]();
getTimeNow();
println("insert 5000 ......");
for indexOne in 1...5000
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.insert(value, atIndex: index);
}
println(someInts.count)
getTimeNow();
println("sort 5000 ......");
sort(someInts, {$0 > $1});
getTimeNow();
println("delete 5000 ......");
for indexOne in 1...5000
{
var value:UInt32 = 0;
if someInts.count > 1
{
value = arc4random() % (UInt32(someInts.count - 1));
}
else
{
value = 0;
}
var index = Int(value);
someInts.removeAtIndex(index);
}
getTimeNow();
println("end ......");
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
swift
insert 500 ......
60.558021068573
sort 500 ......
8.38398933410645
delete 500 ......
23.6489772796631
insert 2000 ......
177.723050117493
sort 2000 ......
39.8159623146057
delete 2000 ......
204.712986946106
insert 5000 ......
668.876051902771
sort 5000 ......
109.582960605621
delete 5000 ......
699.584007263184
end ......
object-c
insert 500 ......
7.395983
sort 500 ......
5.198002
delete 500 ......
3.646970
insert 2000 ......
10.901034
sort 2000 ......
16.789973
delete 2000 ......
6.136000
insert 5000 ......
22.271991
sort 5000 ......
39.604008
delete 5000 ......
13.071001
end ......
Debug版本 时间单位:毫秒
swift
insert 500 ......
11918.8089966774
sort 500 ......
234.127998352051
delete 500 ......
174.408972263336
insert 2000 ......
1107.21004009247
sort 2000 ......
1274.24097061157
delete 2000 ......
1145.07800340652
insert 5000 ......
4589.2099738121
sort 5000 ......
3123.61705303192
delete 5000 ......
4630.10996580124
end ......
object-c
insert 500 ......
7.322013
sort 500 ......
5.365968
delete 500 ......
3.726006
insert 2000 ......
11.458993
sort 2000 ......
16.021013
delete 2000 ......
6.210983
insert 5000 ......
23.373008
sort 5000 ......
39.736032
delete 5000 ......
13.318002
end ......