I have tried many things already, changing up a few things wrote in Swift differently, alternatively using a for instead of the built in 'contains' in swift to see if that might be the problem but Objective-C was still faster.
Objective-C code runs at about - 0.035266s. Swift - around 0.987877011299133s.
Can you guys help me find out what might be wrong with the code? I tried to write both codes identical line for line as much as it was possible, or used the languages given substitutes.
Swift main:
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 CaeserCipher:
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 main:
#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