Skip to content

Instantly share code, notes, and snippets.

@raysarebest
Last active September 20, 2018 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raysarebest/76c7c281da5f9d200b967abe7bcb5379 to your computer and use it in GitHub Desktop.
Save raysarebest/76c7c281da5f9d200b967abe7bcb5379 to your computer and use it in GitHub Desktop.
//
// DynamicTestable.h
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
#ifndef DynamicTestable_h
#define DynamicTestable_h
@import Foundation;
@protocol DynamicTestable
@property (nonatomic, nonnull, readonly) NSString *objectType;
@property (nonatomic) NSInteger property;
-(NSInteger)propertyWithTimes:(NSInteger)times;
@end
#endif /* DynamicTestable_h */
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "ObjectiveCClass.h"
#import "DynamicTestable.h"
test(object: Structure())
test(object: SwiftClass())
test(object: ObjectiveCClass())
divide()
print("Fastest: \(fastest.type)")
//
// ObjectiveCClass.h
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
#import "DynamicTestable.h"
@import Foundation;
@interface ObjectiveCClass : NSObject <DynamicTestable>
@property (nonatomic, nonnull, readonly) NSString *objectType;
@property (nonatomic) NSInteger property;
-(NSInteger)propertyWithTimes:(NSInteger)times;
@end
//
// ObjectiveCClass.m
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
#import "ObjectiveCClass.h"
@implementation ObjectiveCClass
-(NSString *)objectType{
return @"Objective-C Class";
}
-(NSInteger)propertyWithTimes:(NSInteger)times{
return self.property * times;
}
@end
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objects = {
/* Begin PBXBuildFile section */
4CA85B4321543B33002D4FAC /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA85B4221543B33002D4FAC /* main.swift */; };
4CA85B4A21543B6D002D4FAC /* Structure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA85B4921543B6D002D4FAC /* Structure.swift */; };
4CA85B4C21543BA7002D4FAC /* Testable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA85B4B21543BA7002D4FAC /* Testable.swift */; };
4CA85B4E21543E5A002D4FAC /* SwiftClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA85B4D21543E5A002D4FAC /* SwiftClass.swift */; };
4CA85B5221543EAD002D4FAC /* ObjectiveCClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CA85B5121543EAD002D4FAC /* ObjectiveCClass.m */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
4CA85B3D21543B32002D4FAC /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
4CA85B3F21543B33002D4FAC /* fastclass */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = fastclass; sourceTree = BUILT_PRODUCTS_DIR; };
4CA85B4221543B33002D4FAC /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
4CA85B4921543B6D002D4FAC /* Structure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Structure.swift; sourceTree = "<group>"; };
4CA85B4B21543BA7002D4FAC /* Testable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Testable.swift; sourceTree = "<group>"; };
4CA85B4D21543E5A002D4FAC /* SwiftClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftClass.swift; sourceTree = "<group>"; };
4CA85B4F21543EAD002D4FAC /* fastclass-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "fastclass-Bridging-Header.h"; sourceTree = "<group>"; };
4CA85B5021543EAD002D4FAC /* ObjectiveCClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ObjectiveCClass.h; sourceTree = "<group>"; };
4CA85B5121543EAD002D4FAC /* ObjectiveCClass.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ObjectiveCClass.m; sourceTree = "<group>"; };
4CA85B5321544320002D4FAC /* DynamicTestable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DynamicTestable.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
4CA85B3C21543B32002D4FAC /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
4CA85B3621543B32002D4FAC = {
isa = PBXGroup;
children = (
4CA85B4121543B33002D4FAC /* fastclass */,
4CA85B4021543B33002D4FAC /* Products */,
);
sourceTree = "<group>";
};
4CA85B4021543B33002D4FAC /* Products */ = {
isa = PBXGroup;
children = (
4CA85B3F21543B33002D4FAC /* fastclass */,
);
name = Products;
sourceTree = "<group>";
};
4CA85B4121543B33002D4FAC /* fastclass */ = {
isa = PBXGroup;
children = (
4CA85B4221543B33002D4FAC /* main.swift */,
4CA85B5321544320002D4FAC /* DynamicTestable.h */,
4CA85B4B21543BA7002D4FAC /* Testable.swift */,
4CA85B4D21543E5A002D4FAC /* SwiftClass.swift */,
4CA85B5021543EAD002D4FAC /* ObjectiveCClass.h */,
4CA85B5121543EAD002D4FAC /* ObjectiveCClass.m */,
4CA85B4921543B6D002D4FAC /* Structure.swift */,
4CA85B4F21543EAD002D4FAC /* fastclass-Bridging-Header.h */,
);
path = fastclass;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
4CA85B3E21543B32002D4FAC /* fastclass */ = {
isa = PBXNativeTarget;
buildConfigurationList = 4CA85B4621543B33002D4FAC /* Build configuration list for PBXNativeTarget "fastclass" */;
buildPhases = (
4CA85B3B21543B32002D4FAC /* Sources */,
4CA85B3C21543B32002D4FAC /* Frameworks */,
4CA85B3D21543B32002D4FAC /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = fastclass;
productName = fastclass;
productReference = 4CA85B3F21543B33002D4FAC /* fastclass */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
4CA85B3721543B32002D4FAC /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1010;
LastUpgradeCheck = 1010;
ORGANIZATIONNAME = "Michael Hulet";
TargetAttributes = {
4CA85B3E21543B32002D4FAC = {
CreatedOnToolsVersion = 10.1;
LastSwiftMigration = 1010;
};
};
};
buildConfigurationList = 4CA85B3A21543B32002D4FAC /* Build configuration list for PBXProject "fastclass" */;
compatibilityVersion = "Xcode 9.3";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 4CA85B3621543B32002D4FAC;
productRefGroup = 4CA85B4021543B33002D4FAC /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
4CA85B3E21543B32002D4FAC /* fastclass */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
4CA85B3B21543B32002D4FAC /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4CA85B4321543B33002D4FAC /* main.swift in Sources */,
4CA85B4A21543B6D002D4FAC /* Structure.swift in Sources */,
4CA85B5221543EAD002D4FAC /* ObjectiveCClass.m in Sources */,
4CA85B4E21543E5A002D4FAC /* SwiftClass.swift in Sources */,
4CA85B4C21543BA7002D4FAC /* Testable.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
4CA85B4421543B33002D4FAC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
4CA85B4521543B33002D4FAC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
};
name = Release;
};
4CA85B4721543B33002D4FAC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = K3ZLWH83BT;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OBJC_BRIDGING_HEADER = "fastclass/fastclass-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 4.2;
};
name = Debug;
};
4CA85B4821543B33002D4FAC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = K3ZLWH83BT;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@loader_path/../Frameworks",
);
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "fastclass/fastclass-Bridging-Header.h";
SWIFT_VERSION = 4.2;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
4CA85B3A21543B32002D4FAC /* Build configuration list for PBXProject "fastclass" */ = {
isa = XCConfigurationList;
buildConfigurations = (
4CA85B4421543B33002D4FAC /* Debug */,
4CA85B4521543B33002D4FAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4CA85B4621543B33002D4FAC /* Build configuration list for PBXNativeTarget "fastclass" */ = {
isa = XCConfigurationList;
buildConfigurations = (
4CA85B4721543B33002D4FAC /* Debug */,
4CA85B4821543B33002D4FAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 4CA85B3721543B32002D4FAC /* Project object */;
}
//
// Structure.swift
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
struct Structure: StaticTestable{
var objectType: String{
get{
return "Swift Structure"
}
}
var property: Int = 0
func property(times: Int) -> Int {
return property * times
}
}
//
// SwiftClass.swift
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
class SwiftClass: DynamicTestable{
var objectType: String{
get{
return "Swift Class"
}
}
var property: Int = 0
func property(withTimes times: Int) -> Int{
return property * times
}
}
class SwiftNSObject: NSObject, DynamicTestable{
var objectType: String{
get{
return "Swift NSObject Child"
}
}
var property: Int = 0
func property(withTimes times: Int) -> Int{
return property * times
}
}
//
// Testable.swift
// fastclass
//
// Created by Michael Hulet on 9/20/18.
// Copyright © 2018 Michael Hulet. All rights reserved.
//
import Foundation
protocol StaticTestable{ // Swift protocols don't interact well with @objc protocols
var objectType: String{ get }
var property: Int{ get set }
func property(times: Int) -> Int
}
var fastest = (type: "", time: TimeInterval.infinity)
var slowest = (type: "", time: TimeInterval(0))
let testCount = 100_000_000
func divide() -> Void{
print("\n------------------------------------------------------------\n")
}
func test<T: StaticTestable>(object: T) -> Void{
var object = object // This is so we can have mutability
divide()
print(object.objectType)
DispatchQueue(label: object.objectType).sync {
let start = Date()
for _ in 1...testCount{
object.property += 1
let _ = object.property(times: 2);
someFunction(object)
}
let end = Date()
let runtime = end.timeIntervalSince(start)
if runtime < fastest.time{
fastest.time = runtime
fastest.type = object.objectType
}
else if runtime > slowest.time{
slowest.time = runtime
slowest.type = object.objectType
}
print("Runtime: \(runtime) seconds")
}
}
func test<T: DynamicTestable>(object: T) -> Void{
divide()
print(object.objectType)
DispatchQueue(label: object.objectType).sync {
let start = Date()
for _ in 1...testCount{
object.property += 1
let _ = object.property(withTimes: 2);
someFunction(object)
}
let end = Date()
let runtime = end.timeIntervalSince(start)
if runtime < fastest.time{
fastest.time = runtime
fastest.type = object.objectType
}
else if runtime > slowest.time{
slowest.time = runtime
slowest.type = object.objectType
}
print("Runtime: \(runtime) seconds")
}
}
func someFunction<T: StaticTestable>(_ this: T) -> Void{
let _ = this.property
}
func someFunction<T: DynamicTestable>(_ this: T) -> Void{
let _ = this.property
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment