Entering a very large number

bartc bc at freeuk.com
Sun Mar 25 11:00:21 EDT 2018


On 25/03/2018 15:01, Christian Gollwitzer wrote:
> Am 25.03.18 um 14:32 schrieb bartc:
>> Using CPython on my machine, doing a string to int conversion that 
>> specific number took 200 times as long as doing a normal assignment.
>>
>> That conversion took 4 microseconds.
>>
>> Not significant if it's only done once. But it might be executed a 
>> million times.
> 
> Honestly, why should it be executed a million times?

Because it's inside a function that is called a million times?

  Do you have a
> million different 400 digit numbers as constants in your code? If so, I 
> suggest to store them in a database file accompanied with the code.
> 
> If there are few different only, then don't do the conversion a million 
> times. Convert them at module initialization and assign them to a global 
> variable.

That's just another workaround. You don't really want the global 
namespace polluted with names that belong inside functions. And you 
might not want to do all that initialisation on behalf of hundreds of 
functions that may or may not be called. Neither do you want the code at 
module level to be cluttered with all these giant constants.

The real problem is in writing a very long constant that doesn't 
comfortably fit on one screen line, and where the editor used doesn't 
offer any help (in displaying on multiple lines) and neither does the 
language.

-- 
bartc



More information about the Python-list mailing list