gpt4 book ai didi

objective-c - 相同的算法在 Objective-C 中的运行速度比 Swift 快得多(经过优化)

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:20 24 4
gpt4 key购买 nike

我已经尝试了很多东西,以不同的方式改变了一些用 Swift 编写的东西,或者使用 for 而不是 swift 中内置的“包含”来查看这是否可能是问题所在,但 Objective-C 仍然更快。

Objective-C 代码的运行时间约为 - 0.035266 秒。Swift - 大约 0.987877011299133s。

你们能帮我看看代码有什么问题吗?我尝试尽可能多地逐行编写相同的代码,或者使用给定的替代语言。

swift 主要:

    import Foundation

let startDate = NSDate();

let cc = CaesarCipher();


let path = "/Users/Jay/Documents/Projects/CaesarCipher/CaesarCipherSwift/data.txt"
var content = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)

content = content!.lowercaseString;

let arr = cc.cipher(content!, shift: 13);

let endDate = NSDate();

let time = NSTimeInterval(endDate .timeIntervalSinceDate(startDate));

println("ciphered text: \n \(arr) \n");
println("execution time: \n \(time) \n");

Swift 凯撒密码:

import Foundation

class CaesarCipher
{
let alphabet: [Character];
var textToCipher: String;
var shift: Int;

init(){

self.alphabet = ["a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z"];

self.textToCipher = String();
self.shift = Int();
}

func cipher(textToCipher: String, shift: Int)->String{

self.textToCipher = textToCipher;
self.shift = shift;
var newArray: [Character] = [];

for letter in self.textToCipher{

if(contains(alphabet, letter))
{

var modValue = self.shift % 26;
var indexOfLetter = find(alphabet, letter)!;

if(modValue > indexOfLetter){

modValue = modValue-indexOfLetter;
newArray.append(alphabet[alphabet.count-modValue]);
}else{
newArray.append(alphabet[indexOfLetter-modValue]);
}

}else{
newArray.append(" ");
}

}

return String(newArray);

}
}

Objective-C 主要内容:

#import <Foundation/Foundation.h>
#import "CaesarCipher.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *methodStart = [NSDate date];

CaesarCipher *cc = [[CaesarCipher alloc] init];
NSString *path = @"/Users/Jay/Documents/Projects/CaesarCipher/CaesarCipherObjectiveC/data.txt";
NSString *textToCipher =
[[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] lowercaseString];

NSString *cipherText = [cc cipher:textToCipher :13];

NSDate *methodFinish = [NSDate date];

NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];

NSLog(@"%@", cipherText);
NSLog(@"executionTime = %f", executionTime);
}
return 0;
}

Objective-C CaeserCipher.m:

#import <Foundation/Foundation.h>
#import "CaesarCipher.h"

@implementation CaesarCipher
{
NSArray *alphabet;
NSMutableArray *cipherTextArray;
}
-(id)init{

self = [super init];
if(self){

alphabet = [NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g",
@"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s",
@"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];

}
return self;
}

-(NSString *)cipher:(NSString *)textToCipher :(int)shift
{

NSMutableString *text;
text = [NSMutableString string];

for(int i=0; i<textToCipher.length; i++){

NSString *currentLetter =
[NSString stringWithFormat:@"%c", [textToCipher characterAtIndex:i]];

if([alphabet containsObject:currentLetter]){

int modValue = shift % 26;
int indexOfLetter = (int)[alphabet indexOfObject:currentLetter];

if(modValue > indexOfLetter){

modValue = modValue - indexOfLetter;
[text appendString:[alphabet objectAtIndex:(alphabet.count-modValue)]];
}else{

[text appendString:[alphabet objectAtIndex:(indexOfLetter-modValue)]];
}

}else{

[text appendString:@" "];
}

}

return text;
}

@end

Objective-C CaeserCipher.h:

@interface CaesarCipher: NSObject

@property int numerator, denominator;
@property NSString *str;

-(NSString *) cipher: (NSString *) textToCipher: (int) shift;


@end

最佳答案

我很好奇 Swift 与 Objective-C 的性能对比,因此我编写了一个程序来比较两者。

你可以在这里阅读:

Testing Swift's performance against C/Objective-C

事实证明,在关闭编译器优化(这是调试构建的标准设置)的情况下,Swift 代码的性能非常差。应用商店。

我不建议更改调试版本的编译器设置,因为如果这样做,源代码调试会变得很困难。 (编译器可以优化局部变量,它可以改变执行顺序或将多个源代码行混合为一个,等等)

尝试创建一个构建方案来构建发布构建并计时。我敢打赌,您会发现它运行得更快。

您还可以在发布版本上切换build设置以关闭对数组的边界检查。这加快了速度,但它有点危险,并且使调试更加困难。

关于objective-c - 相同的算法在 Objective-C 中的运行速度比 Swift 快得多(经过优化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29445340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com