Last active
November 1, 2021 17:29
-
-
Save sebres/230c4bfafc36c99074202dc59b194a95 to your computer and use it in GitHub Desktop.
Multi-threaded evaluation Tcl vs. Python (with GIL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# IMPORTANT: | |
# note to get correct results you should warm up Tcl and Python, | |
# so better execute it 3 times before you take results for | |
# the comparison | |
## ============================================================ | |
## Tcl | |
## ============================================================ | |
set s { | |
proc test {} { | |
set x 0 | |
while {$x < 100000000} { | |
incr x | |
} | |
} | |
} | |
set c { | |
test | |
} | |
puts [eval $s; timerate $c 1 1] | |
package require Thread | |
set ts { | |
foreach i {0 1 2 3} { | |
set t($i) [thread::create] | |
thread::send $t($i) $s | |
} | |
} | |
set tc { | |
foreach i {0 1 2 3} { | |
thread::send -async $t($i) $c | |
thread::send -async $t($i) {thread::release} | |
} | |
foreach i {0 1 2 3} { | |
catch { thread::release -wait $t($i) } | |
} | |
} | |
puts [eval $ts; timerate $tc 1 1] | |
# ------------------------------------------------------------ | |
# results in (after warming up) : | |
# ------------------------------------------------------------ | |
2391154 µs/# 1 # 0.418 #/sec 2391.154 net-ms | |
2368935 µs/# 1 # 0.422 #/sec 2368.935 net-ms | |
## ============================================================ | |
## Puthon | |
## ============================================================ | |
s=''' | |
def test(): | |
x = 0 | |
while x < 100000000: | |
x += 1 | |
''' | |
c=''' | |
test() | |
''' | |
import timeit | |
print(timeit.timeit(c, s, number=1)*1e6) | |
ts=''' | |
import threading | |
t = list(range(4)) | |
for i in range(4): | |
t[i] = threading.Thread(target=test, args=()) | |
''' | |
tc = ''' | |
for i in range(4): | |
t[i].start() | |
for i in range(4): | |
t[i].join() | |
''' | |
print(timeit.timeit(tc, s+ts, number=1)*1e6) | |
# ------------------------------------------------------------ | |
# results in (after warming up) : | |
# ------------------------------------------------------------ | |
8748484.662999997 | |
35497037.33199999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment