Posted to tcl by mjanssen at Fri Feb 26 07:32:25 GMT 2021view raw
- ## Tcl
- #%timeit
- proc encode {m c} {return "[string length $m]$c"}
- proc say n {
- regsub -all -command {(.)\1*} $n encode
- }
- set s 1321131112
- for {set i 0} {$i < 16} {incr i} {
- set s [say $s]
- }
- puts $s
- 860
- 36676996 microseconds per iteration
- # Python
- %time
- import re
- re_d = re.compile(r'((\d)\2*)')
- def replace(match_obj):
- s = match_obj.group(1)
- return str(len(s)) + s[0]
- s = '1321131112'
- for i in range(16):
- s = re_d.sub(replace,s)
- print(len(s))
- CPU times: user 4 µs, sys: 1 µs, total: 5 µs
- Wall time: 8.82 µs
- 860