"Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> wrote or quoted:
_BitScanForward( &index, *digits ) ;
From the documentation:
|If no bit is found, the function returns 0 and the value
|written to the address in the first parameter is undefined.
. However, if there are 32 0s before a 1 is found, this
might be intended to add "32" to the total zero count.
(GCC's and Clang's "__builtin_ctz" can also count bits and
might be more portable.)
Newsgroups: comp.lang.python,comp.lang.c,sci.math
Followup-To: comp.lang.python,comp.lang.c
125from myrkraverk import count_lsb
print( count_lsb( -( 1 << 125 ) ) )
import timeit
Traceback (most recent call last):timeit.timeit( "count_lsb( 1 << 125 )" )
"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: | Jacob Catayoc |
|---|---|
| Location: | Pasay City, Metro Manila, Philippines |
| Users: | 5 |
| Nodes: | 4 (0 / 4) |
| Uptime: | 498677:07:29 |
| Calls: | 212 |
| Calls today: | 3 |
| Files: | 910 |
| D/L today: |
31 files (22,593K bytes) |
| Messages: | 69,819 |