-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmeasure.py
executable file
·86 lines (71 loc) · 2.87 KB
/
measure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import os
import sys
import json
from timeit import default_timer as timer
# Create and compile n modules.
# In the end, each module will have module{n}/Module{n}.swiftmodule,
# which will be imported later.
def create_modules(number_of_module):
for i in range(number_of_module):
module_dir = f"module{i}"
if os.path.isdir(module_dir):
continue
os.mkdir(module_dir)
source_file = f"{module_dir}/class{i}.swift"
with open(source_file, "w") as f:
f.write(f"public class Class{i}" + " {}")
os.system("xcrun swiftc -c -parse-as-library -emit-module"
+ f" -module-name Module{i} -emit-module-path {module_dir}/Module{i}.swiftmodule"
+ f" {source_file}")
def create_vfsoverlay(import_path, number_of_search_path):
swift_modules = list(map(lambda i: {
"name": f"Module{i}.swiftmodule",
"type": "file",
"external-contents": f"module{i}/Module{i}.swiftmodule",
}, range(number_of_search_path)))
overlay = {
"version": 0,
"roots": [{
"name": import_path,
"type": "directory",
"contents": swift_modules
}]
}
return overlay
def measure_compile_time(number_of_search_path, number_of_import, use_vfsoverlay = False):
create_modules(number_of_search_path)
with open("main.swift", "w") as f:
for i in range(number_of_import):
f.write(f"import Module{i}\n")
f.write('print("Hello, world!")\n')
if use_vfsoverlay:
overlay_file = "vfsoverlay.yaml"
import_path = "/import"
with open(overlay_file, "w") as outfile:
overlay = create_vfsoverlay(import_path, number_of_search_path)
json.dump(overlay, outfile)
import_args = f"-Xfrontend -vfsoverlay -Xfrontend {overlay_file} -I /import"
else:
include_paths = ""
for i in range(number_of_search_path):
include_paths = f"-I module{i} " + include_paths
import_args = include_paths
start = timer()
os.system(f"xcrun swiftc -c {import_args} main.swift")
end = timer()
return end - start
if __name__ == "__main__":
use_vfsoverlay = "--use_vfsoverlay" in sys.argv
for search_path_number in [0, 100, 500, 1000, 1500, 2000]:
for import_number in [0, 100, 500, 1000, 1500, 2000]:
if import_number > search_path_number:
continue
# Build once to warm up the cache (if there is any)
measure_compile_time(search_path_number, import_number, use_vfsoverlay)
total = 0
for i in range(5):
total += measure_compile_time(search_path_number, import_number, use_vfsoverlay)
average = total / 5
print("(include_path: {:4}, import: {:4}): {:2f}"
.format(search_path_number, import_number, average))