DEV Community

Discussion on: Python daily exercise 3: reverse a string

Collapse
 
jmagave profile image
jmagave

Looks like using slice is 4x faster. Timed with %timeit command of YPython.

%timeit s[::-1]
81 ns ± 1.05 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit "".join(reversed(s))
311 ns ± 2.89 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mathewchan profile image
HHMathewChan

just find out your comment, good to know how to count program time, thx