Use Mathematical tricks, can you speed up accounts in Python? [closed]

-5

An example code about my doubt.

%timeit lambda : 10000000000000000*10000000000000000
40.4 ns ± 0.592 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)


%timeit lambda : "10000000000000000"+"10000000000000000"[1:]
40.6 ns ± 0.0841 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
asked by anonymous 26.12.2018 / 17:14

1 answer

3

What you're trying to do does not make sense in Python.

Apart from using a very specific operation, which uses completely different codepaths, it returns different objects, and could not be used for other numbers - Python is a "very high level" language - in general terms, this means that it is separated from what happens in the hardware by many layers, in order to provide simplicity to write the code.

Note that in none of your examples do you have to worry about reserving memory for the strings, checking if the size of the number is compatible with the CPU architecture - and you would not have to worry about dozens of other details if computing which you needed to be more complex.

That's why Python abstracts the data types for you: you deal with "Python integers" that are objects, not data types native to the CPU. The same goes for strings - you do not even have to know what kind of data is used to store each character in a Python string in memory.

All this coupled in a well-optimized runtime over decades makes for many "real-world" operations pretty much makes no difference using Python or a lower-level language: interpolating an HTML template with data that has been read from a SQL database? All custody operations are written in native code, your Python code simply organizes the information.

But if you make a point of not knowing how a computer works and how the language works, you might want to, for example, write code that does integers in pure Python - and then end up with a routine that is about 10000 times slower than native code.

So, optimizations of the kind you're proposing (in spite of the bad example), do not make any sense in Python: multiplication (of numbers that fit into a CPU register) will already be thousands of times slower than made in C, or even in Java, which despite interpreting bytecode as Python, uses native numeric types without translation.

In Python what is done is, after the algorithm is discovered, to use parts that require intensive numerical computation in native libraries - so Python is the language most used to create applications in machine learning eg whole the numerical machinery is made available for use from Python in libraries such as Tensorflow. The same thing if you need more direct numerical calculation: it will use Numpy, which has native code to apply the operation in thousands or millions of numbers at a time in a very optimized way.

But a simple algorithm for numbering in pure Python will be thousands of times slower than in other languages. The way to optimize if you have a certain algorithm in Python, but not implemented in a ready library, is to use Cython, which can optimize Python code as native code, for example.

    
26.12.2018 / 21:10