-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarkdemo.py
81 lines (61 loc) · 2.12 KB
/
benchmarkdemo.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
#!/usr/bin/env python3
"""
Compare the scandir_recursive function against the listdir for depth 0 and
os.walk for different depths and measure the execution times.
"""
import os
import time
from scandirrecursive import scandir_recursive
def walker(path, levels=1):
tree = []
for dirpath, dirnames, filenames in os.walk(path):
if dirpath[len(path) :].count(os.sep) >= levels:
del dirnames[:]
for name in filenames:
tree.append(os.path.join(dirpath, name))
for name in dirnames:
tree.append(os.path.join(dirpath, name))
return tree
def lister(path):
tree = [item for item in os.listdir(path)]
return tree
###############################################################################
###############################################################################
path = "/"
###############################################################################
# DEPTH 0
start = time.time()
tree = list(scandir_recursive(path, depth=0))
end = time.time() - start
print("scandir_recursive (depth=0):")
print("\ttime: {:.3}s".format(end))
start = time.time()
tree = lister(path)
end = time.time() - start
print("listdir (not recursive func):")
print("\ttime: {:.3}s".format(end))
print("-" * 50) ##############################################################
# DEPTH 6
start = time.time()
tree = list(scandir_recursive(path, depth=4))
end = time.time() - start
print("scandir_recursive (depth=4):")
print("\ttime: {:.3}s".format(end))
start = time.time()
tree = walker(path, levels=4)
end = time.time() - start
print("walk_limited (depth=4):")
print("\ttime: {:.3}s".format(end))
print("-" * 50) ##############################################################
# MAX DEPTH
start = time.time()
tree = list(scandir_recursive(path, depth=-1))
end = time.time() - start
print("scandir_recursive (max depth):")
print("\ttime: {:.3}s".format(end))
start = time.time()
tree = walker(path, levels=999999999999)
end = time.time() - start
print("walk (max depth):")
print("\ttime: {:.3}s".format(end))
print("-" * 50) ##############################################################