Why can a computer take minutes to run a couple lines of code, yet can run complex things like games so quickly?

738 views

Why can a computer take minutes to run a couple lines of code, yet can run complex things like games so quickly?

In: Technology

10 Answers

Anonymous 0 Comments

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.

Anonymous 0 Comments

lets say i need to find the biggest person in a group,

if all persons stand in a line from smal to big in one room, then it is a pretty trivial task

if all persons are distributed over multiple rooms and don’t stand in a line next to eachother then it will take more time.

Anonymous 0 Comments

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.

Anonymous 0 Comments

Could you give some examples of a “couple lines of code” that takes minutes?

Because either you’re referring to a loop, like /u/Schnutzel refers to below, or the code is itself just a call into some much much more complicated code which is much longer than a couple lines.

Anonymous 0 Comments

Human readable lines of code needs to be *compiled*, that is, translated to machine readable instructions, and that takes time. Once it is *compiled* it runs faster.

The complex games that you mention were once compiled and that took long time too.

Anonymous 0 Comments

What do you mean by “a couple lines of code”?

Take this code for example:

while (true):
print(“hi”)

These are just two lines of code, but will run endlessly. So it depends on what the program actually does.

Anonymous 0 Comments

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.

Anonymous 0 Comments

Not all code was created equal. Some need more rescourses than others. And some are lower priority.

Anonymous 0 Comments

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)

Anonymous 0 Comments

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.