The following code is just an example to verify the problem. With the Build Configuration Debug, the Swift code takes 7.2 seconds to conclude. The same code with the Build Configuration Debug, but in Objective-C, takes 0.05 seconds.
Objective-C and Swift run fast in the Build Configuration Release, taking 0.001 seconds or less.
I prefer to develop using the Debug configuration. Is there any reason for the Swift code be so slow in that configuration? Is there anything I can change to have a resonable time?
All measures of time were took in a real device.
Here is the test code (Swift 5, Xcode 11.4.1):
let oR = 10
let oG = 20
let oB = 30
let oA = 255
let chkColor = 0xffffffff
let tolerance = 25
var result = false
let startTime = CACurrentMediaTime()
print("Starting...")
for _ in 0 ..< 5000000 {
let cR = (0xff000000 & chkColor) >> 24
let cG = (0x00ff0000 & chkColor) >> 16
let cB = (0x0000ff00 & chkColor) >> 8
let cA = (0x000000ff & chkColor)
result = abs(cR - oR) <= tolerance &&
abs(cG - oG) <= tolerance &&
abs(cB - oB) <= tolerance &&
abs(cA - oA) <= tolerance
}
let currentTime = CACurrentMediaTime()
let elapsedTime = currentTime - startTime
print(result)
print("Time: \(elapsedTime)")
Objective-C:
NSInteger oR = 10;
NSInteger oG = 20;
NSInteger oB = 30;
NSInteger oA = 255;
NSInteger chkColor = 0xffffffff;
NSInteger tolerance = 25;
Boolean result = FALSE;
NSTimeInterval startTime = CACurrentMediaTime();
NSLog(@"Starting...");
for (NSUInteger i = 0; i < 5000000; i++) {
NSInteger cR = (0xff000000 & chkColor) >> 24;
NSInteger cG = (0x00ff0000 & chkColor) >> 16;
NSInteger cB = (0x0000ff00 & chkColor) >> 8;
NSInteger cA = (0x000000ff & chkColor);
result = labs(cR - oR) <= tolerance &&
labs(cG - oG) <= tolerance &&
labs(cB - oB) <= tolerance &&
labs(cA - oA) <= tolerance;
}
NSTimeInterval currentTime = CACurrentMediaTime();
NSTimeInterval elapsedTime = currentTime - startTime;
NSLog(result ? @"True" : @"False");
NSLog(@"Time: %f", elapsedTime);
Thank you!
User contributions licensed under CC BY-SA 3.0