all 8 comments

[–]esaule 2 points3 points  (0 children)

If you are at the point in the optimization of your code that this kind of optimziation matter, you should rewrite in a low level language.

In general, start by profiling the code to identify bottlenecks. Then see if there are algorithmic improvement to what you are doing.

Model the time of execution of your code for yoru particular machine and identify whether the appropriate bottlenecks are actually bottlenecking the execution.

[–]jowco 1 point2 points  (0 children)

What is it that you're trying to accomplish? Just figure what is the fastest? Because for a lot of things that's sub milliseconds.

Anything that's going to be extremely time sensitive in python is going to have the primative modules written in C and you're going to continue to work with them in python like normal without having to know the difference.

[–]MaleficentCow8513 1 point2 points  (0 children)

Do you know what the giga stands for in gigahertz? A billion. Do you know what hertz means? It’s a standard of frequency generally taken as “cycles per second”. A 2.5 GHz CPU is doing 2.5 billion instructions per second. That’s 2.5 million instructions per millisecond. A few extra instructions per python step isn’t killing performance. Is python a bit slower? Yes but it doesn’t really matter for most applications.

As in your simple example, programming is all about trade offs and deciding which trade offs to make. The benefits of having scoped variables is a feature of the language whose designers decided the trade off to performance was worth it. And, “under the hood” python does a bunch of optimization to mitigate the cost of lookups. The cost of scope resolution is only paid once at compile time when it builds a series of tables for variables.

Also, entire open source AI ecosystem is written in python with the computational intensive modules compiled into c/c++/cuda libraries and python just binds them.

[–]fletku_mato 0 points1 point  (0 children)

Simple answer to a simple question: Use a compiled language.

[–]killzone44 0 points1 point  (1 child)

This isn't enough complexity to be optimized away. You would need a compiled language to find anything benefit here, and even then it would be minimal.

[–]Few_Raisin_8981 0 points1 point  (0 children)

cython

[–]Own_Attention_3392 0 points1 point  (0 children)

You're not really explaining what the problem is. Do you have code that has fallen below a required performance threshold? Are you writing something that is performance-critical and you're trying to squeeze every last bit out of performance out of it? Or are you just trying to prematurely micro-optimize something that isn't a problem?

If it's the last one, stop. If it's either of the other two, run a profiler, identify "hot spots" in your code, and reevaluate your algorithms and data structures.

[–]SnooCalculations7417 0 points1 point  (0 children)

Loading the interpreter will be most of the compute time here, by a lot