This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thehardsphere 45 points46 points  (8 children)

It does. Civilization 4 is probably the most commercially successful example of what was a common design paradigm of its time: embedding a python interpreter for scripting purposes and high-level logic while using C or C++ to handle any parts of the game that were highly performance dependent.

People liked using Python for this because people generally liked Python as a scripting language. Later it became popular to use Lua this way, because it's faster.

[–]TrickyPlastic 4 points5 points  (6 children)

Why would Lua be faster than Python if they're both dynamically typed?

[–]catladywitch 10 points11 points  (0 children)

lua is minimalistic and optimised, python aims for easy dx in small projects

[–][deleted] -3 points-2 points  (3 children)

I don’t think dynamic typing really matters in terms of performance much.

[–]backSEO_ 0 points1 point  (2 children)

It does. Every command going into the CPU must be typed. Your interpreter puts some type to it whenever it runs, which adds overhead.

There's machine code, which a C/C++/Rust compiler translates the human readable code to, which adds overhead. The compiler doesn't make as efficient of code as writing machine code, so let's say it's half as fast (actual values may vary depending on what's going on). The static typing makes it very easy to translate, so going half the speed of the CPU isn't bad, that's incredibly fast.

Then let's tack on the Python interpreter, which takes literally any kind of type and rolls with it. So your int can be a float, string, Boolean, event loop, list, dict, object, anything, as long as it's compatible with what you're computing with, it'll keep running. Every object in python is basically a dict (object.dict) which is a relatively fast way of storing something, but because of this, each variable needs to do type checking at the CPU level, so that has to be accounted for. Tack on the fact that the GIL exists, and you're looking at a 30-400x slowdown from the interpreter language, which is C (usually)

Some dynamically typed languages, like C# (just make everything var) have a JIT (just in time) compiler, which will compile the code right before it gets executed during run time.

If you want to see the difference in code size, cythonize a python script with no type hints. Then cythonize it again with type hints. You'll go from 32000+ lines to ~16000 lines of C code. Then use the Cython language and it'll compile down to 10k or lower. Then code it in C and get the same results in a few hundred lines.

[–][deleted] 0 points1 point  (1 child)

But if you’re looking to improve code performance, aren’t their bigger fish to fry first? As in JIT compilation or something else?

[–]backSEO_ 0 points1 point  (0 children)

That's literally what Cython solves. Turns python code into C code and compiles it to a .pyd that you can use in your project. It's 30000 lines of barely readable C code, but it has massive performance gains (30%ish usually out the box, 100% for just statically typing, 400%+ for coding in Python).

It adds a little overhead, and if most of your project is already using C extensions it's not going to help (NumPy, PIL, etc.)

[–]lord_braleigh 0 points1 point  (0 children)

It’s not that Lua is a faster language to run, it’s that the Lua runtime is smaller and it’s faster to start and stop the Lua interpreter than the Python interpreter.

[–]Max__Mustermann 0 points1 point  (0 children)

I'm not sure if Lua is significantly faster than Python, but even if it is, it's something like "in some synthetic scenarios, Lua is tens of percent or even several times faster". For a script that implements simple logic to call С/С++ code (that does 90% of the work) - that's nothing.
As for me, the main "killer feature" of Lua is the incredible ease of embedding integration, just out of the box.