"Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> wrote or quoted:
Perhaps I should have made that clearer in the code comments?
No, I was too focused on just that one line,
not paying attention to the global context.
A bit more worrying is that I do not off hand know how to time my cre-
ation. This is because /timeit/ can't find it.
Maybe you can pass the globals which should contain the name
"count_lsb":
timeit.timeit("count_lsb(1 << 125)", globals=globals())
. The optional "globals" argument specifies a namespace in
which to execute the code.
You could also try the pattern,
start_time = timeit.default_timer()
count_lsb(1<<125)
dt = timeit.default_timer() - start_time
. However, when "count_lsb(1<<125)` is very fast, the results
might be less accurate. (You can ask your chatbot about
"pitfalls of microbenchmarks in Python".)
125000n = 1 << 125000
( n & -n ).bit_length() - 1
... return ( n & -n ).bit_length() - 1def lsb( n ):
3.679828499996802timeit.timeit( "lsb( n )", globals=globals() )
3.6671537999936845timeit.timeit( "lsb( n )", globals=globals() )
3.677232099988032timeit.timeit( "lsb( n )", globals=globals() )
0.8524350000079721timeit.timeit( "count_lsb( n )", globals=globals() )
0.8531503000122029timeit.timeit( "count_lsb( n )", globals=globals() )
0.8298240999865811timeit.timeit( "count_lsb( n )", globals=globals() )
0.8146453999797814timeit.timeit( "count_lsb( n )", globals=globals() )
0.22153766143356377print( 0.8146453999797814 / 3.677232099988032 )
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 30:11:22 |
| Calls: | 1,195 |
| Files: | 1,354 |
| D/L today: |
16 files (19,622K bytes) |
| Messages: | 294,275 |