Entering a very large number

Christian Gollwitzer auriocus at gmx.de
Mon Mar 26 17:04:48 EDT 2018


Am 26.03.18 um 12:31 schrieb bartc:
> On 26/03/2018 10:34, Steven D'Aprano wrote:
>> So what exactly did you do?
> 
> I did this:
> 
> def fn():
>      C = int(
>      "28871482380507712126714295971303939919776094592797"
>      "22700926516024197432303799152733116328983144639225"
>      "94197780311092934965557841894944174093380561511397"
>      "99994215424169339729054237110027510420801349667317"
>      "55152859226962916775325475044445856101949404200039"
>      "90443211677661994962953925045269871932907037356403"
>      "22737012784538991261203092448414947289768854060249"
>      "76768122077071687938121709811322297802059565867")
> 
> #   C = 2887148238050771212671429... [truncated for this post]
> 
>      D=C+C
> 
> for i in range(1000000):
>      fn()
> 
> The purpose was to establish how such int("...") conversions compare in 
> overheads with actual arithmetic with the resulting numbers.

I still claim that it is not necessary to perform the conversion on each 
function call. For instance, the following change speeds it up by a 
factor of 7 on my machine:

=======================================================
def fn(C = int(
"28871482380507712126714295971303939919776094592797"
"22700926516024197432303799152733116328983144639225"
"94197780311092934965557841894944174093380561511397"
"99994215424169339729054237110027510420801349667317"
"55152859226962916775325475044445856101949404200039"
"90443211677661994962953925045269871932907037356403"
"22737012784538991261203092448414947289768854060249"
"76768122077071687938121709811322297802059565867")):
#   C = 2887148238050771212671429... [truncated for this post]

     D=C+C
     return D


for i in range(1000000):
     fn()

#print(fn())
=======================================================

Out of pure laziness, I haven't compared it to the version with the long 
string. Maybe tha gurus here find yet another elegant way to assign the 
constant only once; my trial with "fn.C = foobar" was not successful.

	Christian



More information about the Python-list mailing list