Using Math Tricks, can you speed up Python accounts?

Asked

Viewed 107 times

-3

An example of 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)
  • What is the mathematical trick? By the way, what do you mean by "trick"? In the first you multiply two integers, in the second you add two strings; I don’t understand the relationship between the examples.

  • No, in the second the result is also multiplication!

  • The trick, in this case, would be to add up the ZEROS, to get the same result.

  • But they are very different operations. In one you perform integer multiplication (and you get an integer as a result), in the other you concatenate two strings (and gets a string as a result). I still cannot understand what kind of comparison you want to make.

  • is only a doubt, if computationally, there could be some advantage in runtime, in some specific scenario. I don’t know much about lower-level processing, so the question.

  • and over the string, just pass it to int

  • It doesn’t make sense to me what you’re trying to do. If you want to multiply 2 numbers, use numeric values. The fact of concatenating strings and bringing the "same" result is irrelevant. Even because you are confusing things. A number is a numerical... value. For example, 2 is a number, but can be represented in several ways: like the string "2", or "two", or "two", etc. The fact that a string has the character '2' does not mean that it is the same as number 2 (since the types are different - one is int, another is str). When printing, the result is the "same", but it is "coincidence"

  • 2

    And his timeit is only creating the lambda, but it’s not running, so you’re checking the creation times of Amble. Anyway, one is multiplying numbers and the other is concatenating strings, there’s nothing "math tricks" about it.

  • intendi! thank you.

Show 4 more comments

1 answer

2


What you’re trying to do makes no sense in Python.

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

Note that in none of your examples you have to worry about reserving memory for strings, check if the number size is compatible with the CPU architecture - and would not have to worry about other dozens of details if the computation you need were more complex.

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

All this coupled in a well optimized Runtime over decades makes it virtually no difference to many "real world" operations to use Python or a lower level language: interpolate an HTML template with data that was read from an SQL database? All costly 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 and how the language works, you might want, for example, to write code that accounts with pure Python integers - in which case you end up with a routine that is about 10,000 times slower than native code.

So, optimizations of the kind you’re proposing (despite the bad example), have no sense in Python: the multiplication (of numbers that fit in a CPU register), will already be thousands of times slower than if done in C, or even in Java, which, despite interpreting bytecode as Python, uses native numeric types without translation.

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

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

  • Perfect, were these reasons I wanted to know, thank you very much. now I have a direction.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.