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

741 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

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.

You are viewing 1 out of 10 answers, click here to view all answers.