I tested the speed of looping/iteration in a bunch of languages. Here’s the rough results (fastest to slowest):

  • C++
  • C#
  • JavaScript
  • Pypy
  • CPython (way slower than the rest)

The main takeaway is that if you’re doing a lot of looping (simulations, game engines, robotics, etc) the language that drives your loops matters quite a bit. C++ is king. But if you want a friendlier language, C# is a good choice. If you absolutely want to use Python, Pypy is a decent choice.

I was making a game in Python, and since in games you need to loop through all the game objects every frame, I was worried that python’s slow loop speed might become a bottleneck. I considered making the game in Pypy instead of CPython, but wanted to actually test how much faster iteration/looping is in Pypy, so I experimented. While experimenting, I got curious about a few other languages so I also included C++, C#, and JavaScript in the tests.

Setup

I wanted to test 2 types of loops, a simple one and a more complex one. The simple one does the bare minimum in the loop, it loops 100 million times and just increments a value each time. The more complex one calls a getter and setter on a bunch (10 million) of objects (“entities”).

The code for both the simple and complex loop, for all the languages, is provided in the appendex, for brevity.

Results (Simple Loop)

Here are the raw results for the simple loop:

  • CPython: 4.9 sec
  • Pypy: 0.0.070 sec (~70 times faster than CPython!)
  • JavaScript: 0.074 sec (~65 times faster than CPython!)
  • C#: 0.029 sec (~170 times faster than CPython!)
  • C++: 0.012 sec (~400 times faster than CPython!)

Here is a graphical representation (one that includes CPython and one that doesn’t, for better visibility):

Results (Complex Loop)

And here are the raw results for the complex loop:

  • CPython: 1.0 sec
  • Pypy: 0.063 sec (~15 times faster than CPython!)
  • JavaScript: .039 sec (~25 times faster than CPython!)
  • C#: 0.032 sec (~30 times faster than CPython!)
  • C++: 0.009 sec (~110 times faster than CPython!)

Here is the graphical representation:

Das all folks!

Appendex

Python Code

Simple Loop:

import time

start = time.time()
v = 0
for i in range(100_000_000):
    v += i
end = time.time()

print(f'time: {end - start} sec')
print(v) # to prevent optimizing-out the loop (though python didn't optimize it out anyway! lol)

Complex Loop:

import time

class Entity:
    def __init__(self, value):
        self.value = value
    
    def set(self, value):
        self.value = value

NUM_ENTITIES = 10_000_000

# create entities
entities = []
for i in range(NUM_ENTITIES):
    e = Entity(0)
    entities.append(e)
    
start = time.time()
for i in range(NUM_ENTITIES):
    current = entities[i].value
    entities[i].set(current + 1)
end = time.time()

print(f'time: {end - start} sec')

JavaScript Code

Simple Loop:

var N = 100000000; // 100 mil

var start = new Date().getTime();
var v = 0;
for (var i = 0; i < N; i++) {
  v += i;
}
var end = new Date().getTime();

console.log('time (in seconds) = ' + (end - start) / 1000.0);
console.log('v = ' + v); // prevent optimizing-out the loop

Complex Loop:

class Entity {
    constructor() {
        this._value = 0;
    }
    setValue(value) {
        this._value = value;
    }
    getValue() {
        return this._value;
    }

}

var NUM_ENTITIES = 10000000; // 10 mil

// create entities
var entities = [];
for (var i = 0; i < NUM_ENTITIES; i++) {
    entities.push(new Entity());
}

var start = new Date().getTime();
for (var i = 0; i < NUM_ENTITIES; i++) {
    entities[i].setValue(entities[i].getValue() + 1);
}
var end = new Date().getTime();

console.log('time (in seconds) = ' + (end - start) / 1000.0);

C# Code

Simple Loop:

int N = 100000000; // 100 mil

DateTime start = DateTime.Now;
int v = 0;
for (int i = 0; i < N; i++)
{
   v += i;
}
DateTime end = DateTime.Now;

Console.WriteLine("Time (in seconds) = " + (end - start).TotalSeconds);
Console.WriteLine("v = " + v); // prevent optimizing-out the loop

Complex Loop:

int NUM_ENTITIES = 10000000;

// create entities
Entity[] entities = new Entity[NUM_ENTITIES];
for (int i = 0; i < NUM_ENTITIES; i++)
{
    entities[i] = new Entity();
}

DateTime start = DateTime.Now;
for (int i = 0; i < NUM_ENTITIES; i++)
{
    entities[i].setValue(entities[i].getValue() + 1);
}
DateTime end = DateTime.Now;

Console.WriteLine("Time (in seconds) = " + (end - start).TotalSeconds);

class Entity
{
    public int Value;
    public int getValue()
    {
        return Value;
    }

    public void setValue(int value)
    {
        Value = value;
    }
}

C++ Code

Simple Loop:

#include <iostream>
#include <chrono>

int main()
{
    int N = 100'000'000;

    auto start = std::chrono::high_resolution_clock::now();
    int v = 0;
    for (int i = 0; i < N; i++)
    {
        v += i;
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double> diff = end - start;

    std::cout << "time: " << diff.count() << " s\n";
    std::cout << "v: " << v << "\n"; // to prevent optimizing-out the loop (C++ *did* optimize it out!)
}

Complex Loop:

#include <iostream>
#include <chrono>
#include <vector>

class Entity {
public:
	Entity(int value) : _value(value) {}

	void setValue(int value) { _value = value; }
	int value() const { return _value; }

private:
	int _value;
};

std::vector<Entity> entities;

int main()
{
    int N = 10'000'000;

    // create entities
    for (size_t i = 0; i < N; i++)
    {
        entities.push_back(Entity(0));
    }

    auto start = std::chrono::high_resolution_clock::now();
    for (size_t i = 0; i < N; i++)
    {
        entities[i].setValue(entities[i].value() + 1);
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double> diff = end - start;

    std::cout << "time: " << diff.count() << " s\n";
}