Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #198038

Re: myrkraverk.c

Subject Re: myrkraverk.c
Newsgroups comp.lang.python, comp.lang.c, sci.math, sci.crypt
References (5 earlier) <extension-20260919213739@ram.dialup.fu-berlin.de> <w9OrS.493051$a02.195416@fx11.ams4> <bits-20260920132258@ram.dialup.fu-berlin.de> <BgRrS.204631$Nn1.110223@fx18.ams4> <time-20260920143911@ram.dialup.fu-berlin.de>
From "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid>
Organization Watcom Pro Ltd.
Message-ID <WORrS.156287$NMu2.119921@fx15.ams4> (permalink)
Date 2026-09-20 22:09 +0800

Cross-posted to 4 groups.

Show all headers | View raw


On 9/20/2026 9:48 PM, Stefan Ram wrote:
> "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".)

Thank you, but the following Python session demonstrates the efforts of
my work, and that it is indeed significantly faster than the /tradition-
al/ method.

 >>> n = 1 << 125000
 >>> ( n & -n ).bit_length() - 1
125000
 >>> def lsb( n ):
...     return ( n & -n ).bit_length() - 1
...
 >>> timeit.timeit( "lsb( n )", globals=globals() )
3.679828499996802
 >>> timeit.timeit( "lsb( n )", globals=globals() )
3.6671537999936845
 >>> timeit.timeit( "lsb( n )", globals=globals() )
3.677232099988032
 >>> timeit.timeit( "count_lsb( n )", globals=globals() )
0.8524350000079721
 >>> timeit.timeit( "count_lsb( n )", globals=globals() )
0.8531503000122029
 >>> timeit.timeit( "count_lsb( n )", globals=globals() )
0.8298240999865811
 >>> timeit.timeit( "count_lsb( n )", globals=globals() )
0.8146453999797814

## So now we have actual numbers to work with.  The following snapshot
## demonstrates that my effort is approximately 1/5th to 1/4th of the
## /original/ in a worst case kind of situation.

 >>> print( 0.8146453999797814 / 3.677232099988032 )
0.22153766143356377

## A proper statistician would of course use averages of multiple runs
## of both versions, but I'm sloppy with statistics.  After all, I can
## demonstrate that my method is faster, and not slower, and don't have
## to mess about with the benchmark to give a false impression.

## Of course, I do not know how much of a benefit this is, when it comes
## to real world cryptography, as I have only a very limited set of num-
## bers from Cryptohack so far.  The real test will be in making my own
## Jacobi Symbol function, and benchmark against the one in SymPy.

## I have added sci.math, and sci.crypt, in case they want to comment on
## what kind of real world numbers are typically put through a Jacobi
## Symbol function, when doing cryptographic research.


## Best wishes, and happy cryptography with Python!
-- 
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 01:56 +0800
  Re: ( n & -n ).bit_length() - 1 ## Really? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-19 18:23 +0000
    Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 03:15 +0800
      Re: ( n & -n ).bit_length() - 1 ## Really? Lane W <cactus_DAC@yahoo.com> - 2026-09-19 13:37 -0600
        Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:32 +0800
          Re: ( n & -n ).bit_length() - 1 ## Really? ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-19 20:39 +0000
            Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:56 +0800
            myrkraverk.c (was: Re: ( n & -n ).bit_length() - 1 ## Really?) "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 18:00 +0800
              Re: myrkraverk.c ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-20 12:25 +0000
                Re: myrkraverk.c "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 21:33 +0800
                Re: myrkraverk.c ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-20 13:48 +0000
                Re: myrkraverk.c "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 22:09 +0800
              jacobi_symbol.py (was: Re: myrkraverk.c) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-21 20:34 +0800
                Re: jacobi_symbol.py Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-22 15:20 +0800
                Re: jacobi_symbol.py Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-25 23:32 +0800
          Re: ( n & -n ).bit_length() - 1 ## Really? "Johann \"Myrkraverk\" Oskarsson" <johann@myrkraverk.invalid> - 2026-09-20 04:52 +0800
  Re: ( n & -n ).bit_length() - 1 ## Really? Paul Rubin <no.email@nospam.invalid> - 2026-09-19 14:50 -0700

csiph-web