Skip to content

Commit 6179741

Browse files
committed
ParallelMap
1 parent 07ce8eb commit 6179741

5 files changed

Lines changed: 631 additions & 0 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 46;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
9B42E7E41C4A0CD40097CD7C /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B42E7E31C4A0CD40097CD7C /* main.swift */; };
11+
9B42E7EC1C4A0D090097CD7C /* extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B42E7EA1C4A0D090097CD7C /* extension.swift */; };
12+
9B42E7ED1C4A0D090097CD7C /* test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B42E7EB1C4A0D090097CD7C /* test.swift */; };
13+
/* End PBXBuildFile section */
14+
15+
/* Begin PBXCopyFilesBuildPhase section */
16+
9B42E7DE1C4A0CD40097CD7C /* CopyFiles */ = {
17+
isa = PBXCopyFilesBuildPhase;
18+
buildActionMask = 2147483647;
19+
dstPath = /usr/share/man/man1/;
20+
dstSubfolderSpec = 0;
21+
files = (
22+
);
23+
runOnlyForDeploymentPostprocessing = 1;
24+
};
25+
/* End PBXCopyFilesBuildPhase section */
26+
27+
/* Begin PBXFileReference section */
28+
9B42E7E01C4A0CD40097CD7C /* ParallelMap */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ParallelMap; sourceTree = BUILT_PRODUCTS_DIR; };
29+
9B42E7E31C4A0CD40097CD7C /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
30+
9B42E7EA1C4A0D090097CD7C /* extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = extension.swift; sourceTree = "<group>"; };
31+
9B42E7EB1C4A0D090097CD7C /* test.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = test.swift; sourceTree = "<group>"; };
32+
/* End PBXFileReference section */
33+
34+
/* Begin PBXFrameworksBuildPhase section */
35+
9B42E7DD1C4A0CD40097CD7C /* Frameworks */ = {
36+
isa = PBXFrameworksBuildPhase;
37+
buildActionMask = 2147483647;
38+
files = (
39+
);
40+
runOnlyForDeploymentPostprocessing = 0;
41+
};
42+
/* End PBXFrameworksBuildPhase section */
43+
44+
/* Begin PBXGroup section */
45+
9B42E7D71C4A0CD40097CD7C = {
46+
isa = PBXGroup;
47+
children = (
48+
9B42E7E21C4A0CD40097CD7C /* ParallelMap */,
49+
9B42E7E11C4A0CD40097CD7C /* Products */,
50+
);
51+
sourceTree = "<group>";
52+
};
53+
9B42E7E11C4A0CD40097CD7C /* Products */ = {
54+
isa = PBXGroup;
55+
children = (
56+
9B42E7E01C4A0CD40097CD7C /* ParallelMap */,
57+
);
58+
name = Products;
59+
sourceTree = "<group>";
60+
};
61+
9B42E7E21C4A0CD40097CD7C /* ParallelMap */ = {
62+
isa = PBXGroup;
63+
children = (
64+
9B42E7E31C4A0CD40097CD7C /* main.swift */,
65+
9B42E7EA1C4A0D090097CD7C /* extension.swift */,
66+
9B42E7EB1C4A0D090097CD7C /* test.swift */,
67+
);
68+
path = ParallelMap;
69+
sourceTree = "<group>";
70+
};
71+
/* End PBXGroup section */
72+
73+
/* Begin PBXNativeTarget section */
74+
9B42E7DF1C4A0CD40097CD7C /* ParallelMap */ = {
75+
isa = PBXNativeTarget;
76+
buildConfigurationList = 9B42E7E71C4A0CD40097CD7C /* Build configuration list for PBXNativeTarget "ParallelMap" */;
77+
buildPhases = (
78+
9B42E7DC1C4A0CD40097CD7C /* Sources */,
79+
9B42E7DD1C4A0CD40097CD7C /* Frameworks */,
80+
9B42E7DE1C4A0CD40097CD7C /* CopyFiles */,
81+
);
82+
buildRules = (
83+
);
84+
dependencies = (
85+
);
86+
name = ParallelMap;
87+
productName = ParallelMap;
88+
productReference = 9B42E7E01C4A0CD40097CD7C /* ParallelMap */;
89+
productType = "com.apple.product-type.tool";
90+
};
91+
/* End PBXNativeTarget section */
92+
93+
/* Begin PBXProject section */
94+
9B42E7D81C4A0CD40097CD7C /* Project object */ = {
95+
isa = PBXProject;
96+
attributes = {
97+
LastSwiftUpdateCheck = 0720;
98+
LastUpgradeCheck = 0720;
99+
ORGANIZATIONNAME = zxy;
100+
TargetAttributes = {
101+
9B42E7DF1C4A0CD40097CD7C = {
102+
CreatedOnToolsVersion = 7.2;
103+
};
104+
};
105+
};
106+
buildConfigurationList = 9B42E7DB1C4A0CD40097CD7C /* Build configuration list for PBXProject "ParallelMap" */;
107+
compatibilityVersion = "Xcode 3.2";
108+
developmentRegion = English;
109+
hasScannedForEncodings = 0;
110+
knownRegions = (
111+
en,
112+
);
113+
mainGroup = 9B42E7D71C4A0CD40097CD7C;
114+
productRefGroup = 9B42E7E11C4A0CD40097CD7C /* Products */;
115+
projectDirPath = "";
116+
projectRoot = "";
117+
targets = (
118+
9B42E7DF1C4A0CD40097CD7C /* ParallelMap */,
119+
);
120+
};
121+
/* End PBXProject section */
122+
123+
/* Begin PBXSourcesBuildPhase section */
124+
9B42E7DC1C4A0CD40097CD7C /* Sources */ = {
125+
isa = PBXSourcesBuildPhase;
126+
buildActionMask = 2147483647;
127+
files = (
128+
9B42E7E41C4A0CD40097CD7C /* main.swift in Sources */,
129+
9B42E7ED1C4A0D090097CD7C /* test.swift in Sources */,
130+
9B42E7EC1C4A0D090097CD7C /* extension.swift in Sources */,
131+
);
132+
runOnlyForDeploymentPostprocessing = 0;
133+
};
134+
/* End PBXSourcesBuildPhase section */
135+
136+
/* Begin XCBuildConfiguration section */
137+
9B42E7E51C4A0CD40097CD7C /* Debug */ = {
138+
isa = XCBuildConfiguration;
139+
buildSettings = {
140+
ALWAYS_SEARCH_USER_PATHS = NO;
141+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
142+
CLANG_CXX_LIBRARY = "libc++";
143+
CLANG_ENABLE_MODULES = YES;
144+
CLANG_ENABLE_OBJC_ARC = YES;
145+
CLANG_WARN_BOOL_CONVERSION = YES;
146+
CLANG_WARN_CONSTANT_CONVERSION = YES;
147+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
148+
CLANG_WARN_EMPTY_BODY = YES;
149+
CLANG_WARN_ENUM_CONVERSION = YES;
150+
CLANG_WARN_INT_CONVERSION = YES;
151+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
152+
CLANG_WARN_UNREACHABLE_CODE = YES;
153+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
154+
CODE_SIGN_IDENTITY = "-";
155+
COPY_PHASE_STRIP = NO;
156+
DEBUG_INFORMATION_FORMAT = dwarf;
157+
ENABLE_STRICT_OBJC_MSGSEND = YES;
158+
ENABLE_TESTABILITY = YES;
159+
GCC_C_LANGUAGE_STANDARD = gnu99;
160+
GCC_DYNAMIC_NO_PIC = NO;
161+
GCC_NO_COMMON_BLOCKS = YES;
162+
GCC_OPTIMIZATION_LEVEL = 0;
163+
GCC_PREPROCESSOR_DEFINITIONS = (
164+
"DEBUG=1",
165+
"$(inherited)",
166+
);
167+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
168+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
169+
GCC_WARN_UNDECLARED_SELECTOR = YES;
170+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
171+
GCC_WARN_UNUSED_FUNCTION = YES;
172+
GCC_WARN_UNUSED_VARIABLE = YES;
173+
MACOSX_DEPLOYMENT_TARGET = 10.11;
174+
MTL_ENABLE_DEBUG_INFO = YES;
175+
ONLY_ACTIVE_ARCH = YES;
176+
SDKROOT = macosx;
177+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
178+
};
179+
name = Debug;
180+
};
181+
9B42E7E61C4A0CD40097CD7C /* Release */ = {
182+
isa = XCBuildConfiguration;
183+
buildSettings = {
184+
ALWAYS_SEARCH_USER_PATHS = NO;
185+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
186+
CLANG_CXX_LIBRARY = "libc++";
187+
CLANG_ENABLE_MODULES = YES;
188+
CLANG_ENABLE_OBJC_ARC = YES;
189+
CLANG_WARN_BOOL_CONVERSION = YES;
190+
CLANG_WARN_CONSTANT_CONVERSION = YES;
191+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
192+
CLANG_WARN_EMPTY_BODY = YES;
193+
CLANG_WARN_ENUM_CONVERSION = YES;
194+
CLANG_WARN_INT_CONVERSION = YES;
195+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
196+
CLANG_WARN_UNREACHABLE_CODE = YES;
197+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
198+
CODE_SIGN_IDENTITY = "-";
199+
COPY_PHASE_STRIP = NO;
200+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
201+
ENABLE_NS_ASSERTIONS = NO;
202+
ENABLE_STRICT_OBJC_MSGSEND = YES;
203+
GCC_C_LANGUAGE_STANDARD = gnu99;
204+
GCC_NO_COMMON_BLOCKS = YES;
205+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
206+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
207+
GCC_WARN_UNDECLARED_SELECTOR = YES;
208+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
209+
GCC_WARN_UNUSED_FUNCTION = YES;
210+
GCC_WARN_UNUSED_VARIABLE = YES;
211+
MACOSX_DEPLOYMENT_TARGET = 10.11;
212+
MTL_ENABLE_DEBUG_INFO = NO;
213+
SDKROOT = macosx;
214+
};
215+
name = Release;
216+
};
217+
9B42E7E81C4A0CD40097CD7C /* Debug */ = {
218+
isa = XCBuildConfiguration;
219+
buildSettings = {
220+
PRODUCT_NAME = "$(TARGET_NAME)";
221+
};
222+
name = Debug;
223+
};
224+
9B42E7E91C4A0CD40097CD7C /* Release */ = {
225+
isa = XCBuildConfiguration;
226+
buildSettings = {
227+
PRODUCT_NAME = "$(TARGET_NAME)";
228+
};
229+
name = Release;
230+
};
231+
/* End XCBuildConfiguration section */
232+
233+
/* Begin XCConfigurationList section */
234+
9B42E7DB1C4A0CD40097CD7C /* Build configuration list for PBXProject "ParallelMap" */ = {
235+
isa = XCConfigurationList;
236+
buildConfigurations = (
237+
9B42E7E51C4A0CD40097CD7C /* Debug */,
238+
9B42E7E61C4A0CD40097CD7C /* Release */,
239+
);
240+
defaultConfigurationIsVisible = 0;
241+
defaultConfigurationName = Release;
242+
};
243+
9B42E7E71C4A0CD40097CD7C /* Build configuration list for PBXNativeTarget "ParallelMap" */ = {
244+
isa = XCConfigurationList;
245+
buildConfigurations = (
246+
9B42E7E81C4A0CD40097CD7C /* Debug */,
247+
9B42E7E91C4A0CD40097CD7C /* Release */,
248+
);
249+
defaultConfigurationIsVisible = 0;
250+
};
251+
/* End XCConfigurationList section */
252+
};
253+
rootObject = 9B42E7D81C4A0CD40097CD7C /* Project object */;
254+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// extension.swift
3+
// 利息
4+
//
5+
// Created by 张星宇 on 16/1/16.
6+
// Copyright © 2016年 张星宇. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Array {
12+
func ktMap<T>(transform: (Element) -> T) -> [T] {
13+
guard !self.isEmpty else { return [] } // 如果数组为空就直接返回空数组
14+
15+
var result: [(Int, [T])] = []
16+
let group = dispatch_group_create()
17+
let syncQueue = dispatch_queue_create("com.gcd.kt", DISPATCH_QUEUE_SERIAL)
18+
19+
let subArrayLength: Int = max(1, self.count / NSProcessInfo.processInfo().activeProcessorCount / 2)
20+
21+
for var step = 0; step * subArrayLength < self.count; step++ {
22+
var stepResult: [T] = []
23+
let localStep = step
24+
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
25+
for i in (localStep * subArrayLength)..<min(((localStep + 1) * subArrayLength), self.count) {
26+
stepResult += [transform(self[i])]
27+
}
28+
29+
dispatch_group_async(group, syncQueue) {
30+
result += [(localStep, stepResult)]
31+
}
32+
}
33+
}
34+
35+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
36+
return result.sort { $0.0 < $1.0 }.flatMap { $0.1 }
37+
}
38+
}

ParallelMap/ParallelMap/main.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// main.swift
3+
// 利息
4+
//
5+
// Created by 张星宇 on 16/1/16.
6+
// Copyright © 2016年 张星宇. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
func ktTimer<T>(description: String, @autoclosure task performTask: () -> T) -> Void {
12+
let start = NSDate().timeIntervalSince1970
13+
performTask()
14+
15+
let duration = NSDate().timeIntervalSince1970 - start
16+
print("Mission '\(description)' completed in \(duration * 1000) ms")
17+
}
18+
19+
func transformGenerater(duration duration: Float) -> Int -> Int {
20+
return {
21+
NSThread.sleepForTimeInterval(NSTimeInterval(duration))
22+
return $0 * 2
23+
}
24+
}
25+
26+
/// 正确性测试
27+
let test = UnitTest()
28+
test.beginTest()
29+
30+
/// 性能测试
31+
let littleArray: Array<Int> = [1,2,3,4,5,6,7,8,9,10] // 模拟数据量小的情况
32+
var largeArray: Array<Int> = [] // 模拟数据量大的情况
33+
34+
for i in 0..<1000 {
35+
largeArray.append(i)
36+
}
37+
38+
ktTimer("1.少量数据,多线程map方法,耗时计算", task: littleArray.ktMap(transformGenerater(duration: 0.01)))
39+
ktTimer("2.少量数据,普通map方法,耗时计算", task: littleArray.map(transformGenerater(duration: 0.01)))
40+
ktTimer("3.少量数据,多线程map方法,不耗时计算", task: littleArray.ktMap(transformGenerater(duration: 0)))
41+
ktTimer("4.少量数据,普通map方法,不耗时计算", task: littleArray.map(transformGenerater(duration: 0)))
42+
print("")
43+
ktTimer("5.大量数据,多线程map方法,耗时计算", task: largeArray.ktMap(transformGenerater(duration: 0.01)))
44+
ktTimer("6.大量数据,普通map方法,耗时计算", task: largeArray.map(transformGenerater(duration: 0.01)))
45+
ktTimer("7.大量数据,多线程map方法,不耗时计算", task: largeArray.ktMap(transformGenerater(duration: 0)))
46+
ktTimer("8.大量数据,普通map方法,不耗时计算", task: largeArray.map(transformGenerater(duration: 0)))
47+

0 commit comments

Comments
 (0)