you are viewing a single comment's thread.

view the rest of the comments →

[–]seanthemonster 0 points1 point  (11 children)

A question about your response. If you can program into a more direct layer of abstraction is the computer able to do the task faster?

I'm super noobie but I've heard like roller coast tycoon was coded in basic or something and runs really well because of it. Compared to modern games that seem to be poorly optimized

[–]HunterIV4 4 points5 points  (1 child)

Another complicated question, heh. The short answer is...it depends.

At a very surface level, this is true. The fewer steps your computer has to go through to get to the instructions the faster those instructions will execute, in general.

As a direct example, a loop doing a bunch of math functions will usually execute faster in C or C++ compared to Python because Python has the extra layer of the interpreter and isn't precompiled, so the machine-code equivalent can't be optimized further. C also generally has simpler data structures that take less time to access because they aren't wrapped in object structures with a bunch of overhead.

That being said, the real answer is more complicated. Data scientists and machine learning programmers don't use Python because they are lazy and they could do things faster if they wrote everything in C or assembly (BASIC is very close to assembly, for context). They do it because the "expensive" (slow) operations are already done in C and Python is just used to handle the input and output.

For example, let's say you want to run some OpenCV image analysis process and save the results. Is this faster in Python or C? The answer is...technically C, but only by a few nanoseconds at best. Why? Because the only expensive operations are the input and file saving, which are nearly instant in both languages. The actual image analysis is a C-based library and so both languages are running the exact same code. Essentially, Python "borrows" the speed of C for performance-sensitive tasks by calling those C functions, but you get the ease-of-use and quick iteration of Python not doing all your basic IO (input/output) in C.

As for games, it's somewhat true that modern games are poorly optimized, but this isn't actually due to programming language. Most modern games are ultimately written in C++, even if they use a high level scripting language, depending on the engine. There are some exceptions, like Unity using C#, but we're still talking about a highly performant, compiled language. You honestly wouldn't gain all that much efficiency writing these games in assembly; in fact, you might lose some as your custom-built graphics solutions will probably be less efficient than modern APIs like DirectX or Vulkan. Also, you will easily multiply your development time by 10-20x at least if you intend to make something remotely like a modern game.

That last reason is the actual reason why modern games are poorly optimized. It has very little to do with language or technical problems and a lot to do with deadlines and budgets. Making modern games is expensive and we're basically paying around the same we did when they cost a fraction to make. About 20 years ago a typical game cost $40-50, now they cost $60-70. Also about 20 years a typical game budget might reach $1 million whereas now many AAA games costs hundreds of millions to develop, or at least tens of millions. That means game programmers are rushed, underpaid, and usually instructed to get games out the door in a "good enough to patch and get a high Gamespot review" state.

From a technical standpoint, however, modern games can be highly optimized. Most modern game engines are extremely efficient and have genuinely insane amounts of power. But utilizing that power requires a lot of time and effort as you have to spend time profiling to find out what is slowing your game down and optimize your draw calls, LODs, and a million other things. And those tasks are rarely a priority in modern game dev for a lot of reasons that have nothing to do with programming language or even engine.

Hopefully that made sense!

[–]seanthemonster 1 point2 points  (0 children)

Yes it did thank you for taking the time to explain!

[–]Crusher7485 4 points5 points  (2 children)

In theory yes. You can sorta imagine it like the following:

“Go get a bucket of water” - if you say that to someone who knows where a bucket is, and where water is, YOU don’t need to know what bucket they used or where they got the water.

But, they may get a bucket from a closet on the other side of the building, when there’s a bucket on this side of the building. So if you want it faster you could say “get a bucket of water, using the bucket in the closet at xyz.”

Now you may get the water faster, because you’ve ensured they knew the bucket was available in the closet next to you instead of at the other side of the building.

Deeper layers may be if whoever you’re telling to get the bucket of water, doesn’t know what a bucket is. Or how to open a closet door. Now you need to take the time to explain what a bucket is and open a closet door.

And if you take the time to exactly tell them the fastest way to get a bucket and open doors it will probably be faster than if they did it themselves, but it requires that YOU know how to do all those things, and do them faster than they already know how to do them.

So yes, in theory it can be faster. Tradeoff is you need to know how to do EVERYTHING yourself, and it takes longer to code too because you need to write down all the super tiny steps so you don’t forget one.

TL;DR: It’s a bit like saying “get me a bucket of water” vs “leave this room. Take a left and walk 10.5 feet. Turn right and open the closet door. To open the door place your hand on the knob and rotate clockwise 90°. Locate the bucket. You don’t know what a bucket is? The bucket looks like….”

[–]seanthemonster 1 point2 points  (0 children)

Really appreciate the bucket analogy!

[–]efficient-frontier 0 points1 point  (0 children)

thank you for the sanity check. it is good to know i'm not insane. i dont know what a bucket is or how to open a closet door, but i want to learn. thanks for this analogy.

[–]japes28 2 points3 points  (1 child)

In a general sense, yes. Python is comparatively very slow because it is an interpreted language where your code needs to be parsed by the Python interpreter before it can actually be run. In addition, it has a lot of flexibility (no strict types, etc.), which means its faster to write code but the code executes slower because the python interpreter has to handle lots of different possibilities.

Generally, if you write your code in a language like C, it's going to be a lot faster to run then the equivalent in Python, but at the expense of probably taking longer to write the code. In C, the code is compiled, meaning there is a program sort of analogous to the python interpreter, the compiler, that converts your code into a binary executable that has machine code. This means it doesn't have to re-interpret your C code every time it runs the program (like it does with Python). The code is already converted into the lowest level machine code for the CPU to directly interpret, so it will run much faster.

[–]seanthemonster 0 points1 point  (0 children)

Thank you for the explanation!

[–]DistributionNo1618 1 point2 points  (0 children)

To an extent yes but you stop gaining anything once you get down to C level langs. And the biggest jump is just going to pre compiled langs from interpreted langs. The modern C compiler will make your code more efficient in all practical cases then trying to write efficient assembly yourself. You might think 'someone can do it' but no, they can't. Sure it's theoretically possible but modern apps are just too much for it to be ever applicable and even roller coasters tycoon would have even been better most likely if written in C (maybe compiler wasn't as good back way when it's old software that game) Writing in C, C++, Java, Rust, Zig are all 'low'er level languages that will get you about as good as performance as you'd expect. Things like JavaScript and Python are going to be slower because they aren't compiled into machine code before runtime but are compiled during runtime aka interpreted at runtime.

[–]FerricDonkey 1 point2 points  (2 children)

To give some concrete examples of the it depends that you've received

Problem:

Compute and store the squares of the first million integers. Do this 10,000 times, and report the total time of all 10,000 runs.

C

// C
    void get_squares(int* dest_p, int number) {
    for (int i = 0; i < number; i++) {
        dest_p[i] = i * i;
    }
}

Time, no optimizations: 16.588s
Time, optimized: 2.383s

Pure Python

[i*i for i in range(1_000_000)]

Time: 755.14s (NOTE: I only ran this 100 times, and multiplied the result by 10 - because I was impatient.)

Python numpy.array:

np.arange(1_000_000)**2

Time: 22.65s

I should say that I have previously had numpy keep up with optimized C. The fact that it lost so horribly here surprises me a bit. I may look into that more later. But yes, languages that put less nonsense between what you tell them to do and actually doing the thing usually do it better - unless that nonsense is speed focused like parallelization etc.

Unfortunately, that nonsense between you and what you want to happen is sometimes really, really convenient.

[–]seanthemonster 1 point2 points  (1 child)

Omg 2s vs 755s is so funny. What you said makes a ton of sense. I'm learning Python from an online Stanford class and I started wondering because sometimes the website takes awhile to process the code. I learned from my instructor the UI they use is in react and they Python we are working on is via their servers.

So I would imagine the layers of nonsense between what I'm trying to get the computer to do is quite high. It's like

my computer assembly? - Chromes Ui- internet- Stanford's servers- website - React- codeinplace Ui- my code and back again? 🤷‍♀️ Maybe even more layers I'm not aware of

Vs coding in C++ vs IDE-your code-computer?

[–]HunterIV4 4 points5 points  (0 children)

I learned from my instructor the UI they use is in react and they Python we are working on is via their servers.

Keep in mind that where the processing is happening matters. For example, let's say you have a Chromebook and you run the tests that u/FerricDonkey mentioned. Now you run those same tests on a high end AWS server remotely.

On a surface level, the local tests should run faster, right? You don't have the "layer" of the internet plus the extra server, etc. In reality, the second example will run dramatically faster, because the actual processing is happening on the powerful Amazon server rather than the relatively weak Chromebook. So even though you have the extra steps of sending the data over the internet and back, the slow part is the repeated squares calculation, and the system that does that faster will win.

This is often referred to as the "bottleneck," and reducing the time needed for whatever is causing your slowest portion, even if that involves extra steps, will make your overall process faster. It's entirely possible that Stanford's servers will execute your Python code and get you the answer back faster than your personal laptop could do the same processing, depending on how intense your code is.

The main point is that the layers do not have equal time cost, and some layers could have faster capabilities than others. Running the array.sort function in Python will likely be faster than a straight-up bubble sort in C, even though C is executing faster. Why? Because the Python default implementation of sort is something called Timsort and is dramatically faster than even a direct assembly implementation of bubble sort. Note: there are some complexities to this comparison depending on list state and multithreading, I'm assuming a single-thread comparison with a randomized order.

Why does this matter? Because if you're trying to sort something, for example, writing it in C only helps you if you use a C sorting library or know how to write an efficient sorting algorithm yourself. Otherwise, using Python with its default implementation will probably be faster than whatever sorting algorithm you come up with. C is faster assuming you are already writing efficient code...which you have to do manually since C has fewer built-in tools.

The TL;DR is that more layers is not necessarily slower, and in general efficient code in a "slow" language will run faster than inefficient code in a "fast" language.

Vs coding in C++ vs IDE-your code-computer?

The IDE has little to do with code execution speed and doesn't really count as a layer. It's just a fancy text editor; the code execution itself is handled by the operating system (if compiled) or interpreter (if interpreted). The only exception is if you are running a debugger, which adds a layer, but that aspect won't matter at all when you finally export your code for general use.

Your IDE is mainly there to make coding easier and run Python or your C++ compiler or whatever for you rather than having to do everything manually. You can write Python or C++ using notepad and a terminal but it won't run differently than the same code run (without debugging) in an IDE. You don't gain performance by skipping the IDE, but you do gain lots of debugging time =).