I read your comments and I think I see your point. One of the limiting factors with what you are talking about is having to finish the first loop before starting the second. In a video game, a bunch of things are happening simultaneously. Imagine instead that you had two lines that had to be loop 10000 times each, or one line looped 20000 times. The first one would be way faster.
The number of lines of code a computer is told to work on are in now way in any sort of relation to the amount of time it takes to work through them.
It is a lot like giving instructions to a human being.
Simple orders can nonetheless involve a lot of work for the person told to execute them.
Telling someone to “dig a hole”, digging a hole is harder though. and ordering someone to “dig a hundred holes” is only an order a slight bit longer and more complex but it will take hundred times longer for a single person to execute.
If a computer is taking minutes to run a couple of lines of code, than either of those lines of code are incredibly damn complex in terms of what they actually require the computer to do, or the computer is running a bunch of other stuff in the background at the same time that is slowing it down.
Not all lines of code are going to be equally complex just like not all word problems in math are going to be equally complex. It takes about as much characters to ask someone to find out what seven taken away away from three and twenty as it does to ask for the square root of three billion and two hundred and sixty-five. A computer or a person would have a much harder time with the latter than the former.
One thing which I can’t see being covered in other comments is parallel processing. Most games / graphic based softwares use the GPU, functioning of GPU is very different from a CPU. It has a large pool of lower clock speed (lower performance) processors. If I were to figure out the color of a pixel to be displayed in an intense fighting game, I would dedicate a single part of GPU to it, there can be millions of these on a current gen GPU. This is how it can calculate multiple frames in the time taken to display a single frame.
A typical code is run on a CPU which has a much higher clock speed but a single processor being used for the execution, so it can take longer (can take shorter too if the code can’t be made to execute in parallel)
Those couple of lines of code run on the computer’s processor, called the CPU. A CPU runs one command at a time, more or less.
Your video game, on the other hand, runs on a GPU (a graphics processing unit). This component contains many processors and runs multiple commands in parallel (that is, at the same time), and has code written especially to take advantage of that fact. In video games, typically the same operations need done for every pixel of the screen (What can be seen at that pixel? What colour is it? Is it in shadow? etc) and so processing the whole screen in parallel rather than a pixel at a time is much more efficient and gives the high frame rate needed for a game.
There are programming languages, such as CUDA, that allow you to write code that runs in parallel on a GPU. If your code that loops 100 million times is doing operations that are independent of one another, rather than the result of one depending on the result from the previous time round the loop, then you can parallelise your code to make it run many times faster.
Latest Answers