Performance: measured, not assumed
Three measurements that cover the three things a program spends time on: comparing and moving data (sorting ten million integers), reading memory once (hashing 64 MB), and creating a million objects. The same input, the same algorithms and the same machine, with g++ at -O2 and go build at its default, which is already an optimising build.
One program, three measurements
The generator is a linear congruential generator, identical in both programs, so the array to sort is the same. The hash is FNV-1a over the same 64 MB of 0x5a, and both programs print the hash so a reader can see that they did the same work. The allocation loop creates a million records and, in C++, frees them; in Go it drops the slice and calls the collector once.
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <vector>
using Clock = std::chrono::steady_clock;
struct Record {
std::int64_t id;
double value;
};
long long ms(Clock::time_point start)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start).count();
}
int main()
{
constexpr int n = 10000000;
std::vector<int> values(n);
std::uint32_t state = 12345;
for (int& value : values) {
state = state * 1664525u + 1013904223u;
value = static_cast<int>(state >> 8);
}
auto start = Clock::now();
std::sort(values.begin(), values.end());
std::cout << "sort: " << ms(start) << " ms\n";
constexpr std::size_t size = 64u << 20;
std::vector<std::uint8_t> buffer(size, 0x5a);
start = Clock::now();
std::uint64_t hash = 14695981039346656037ull;
for (std::uint8_t byte : buffer) {
hash ^= byte;
hash *= 1099511628211ull;
}
std::cout << "hash: " << ms(start) << " ms (" << hash << ")\n";
start = Clock::now();
std::vector<Record*> records;
records.reserve(1000000);
for (std::int64_t i = 0; i < 1000000; ++i) {
records.push_back(new Record{i, static_cast<double>(i) * 0.5});
}
long long allocated = ms(start);
start = Clock::now();
for (Record* record : records) {
delete record;
}
std::cout << "alloc: " << allocated << " ms (" << ms(start) << " ms to free)\n";
}
package main
import (
"fmt"
"runtime"
"slices"
"time"
)
type Record struct {
id int64
value float64
}
func ms(start time.Time) int64 {
return time.Since(start).Milliseconds()
}
func main() {
const n = 10000000
values := make([]int, n)
state := uint32(12345)
for i := range values {
state = state*1664525 + 1013904223
values[i] = int(state >> 8)
}
start := time.Now()
slices.Sort(values)
fmt.Printf("sort: %d ms\n", ms(start))
const size = 64 << 20
buffer := make([]byte, size)
for i := range buffer {
buffer[i] = 0x5a
}
start = time.Now()
var hash uint64 = 14695981039346656037
for _, b := range buffer {
hash ^= uint64(b)
hash *= 1099511628211
}
fmt.Printf("hash: %d ms (%d)\n", ms(start), hash)
start = time.Now()
records := make([]*Record, 0, 1000000)
for i := range int64(1000000) {
records = append(records, &Record{id: i, value: float64(i) * 0.5})
}
allocated := ms(start)
start = time.Now()
records = nil
runtime.GC()
fmt.Printf("alloc: %d ms, free and GC: %d ms\n", allocated, ms(start))
}
The numbers
g++ -std=c++23 -O2 -Wall -Wextra bench.cpp -o bench
./bench
sort: 1382 ms
hash: 136 ms (10696837084269912869)
alloc: 71 ms (42 ms to free)
go build -o bench .
./bench
sort: 1705 ms
hash: 138 ms (10696837084269912869)
alloc: 110 ms, free and GC: 4 ms
| Measurement | C++, run 1 / 2 | Go, run 1 / 2 | Ratio |
|---|---|---|---|
| Sort 10 million integers | 1382 / 1369 ms | 1705 / 1861 ms | C++ 1.25 to 1.35 times faster |
| FNV-1a over 64 MB | 136 / 136 ms | 138 / 149 ms | Equal within noise |
| Allocate 1 million records | 71 ms | 110 ms | C++ 1.5 times faster |
| Release the same records | 42 ms | 4 ms (one GC) | Not comparable, see below |
What the numbers mean
A quarter to a third on sorting is a real difference and the one a C++ programmer expects. Both standard sorts are introsort-like, and the Go one carries bounds checks on the slice and a generic implementation the compiler must specialise. It is not the ten times that a benchmark of hand-written C against a naive Go loop would show, because neither program is naive.
The hash loop is the interesting row. Go compiles the same loop to about the same machine code, the bounds check on buffer[i] is eliminated because the range gives the length, and the result is within one percent. A tight loop over a slice is not where a managed language loses.
Allocation is where the runtime shows: 71 against 110 milliseconds for the same million records. Freeing is the other side: C++ walks the pointers and calls the allocator, which took 42 ms; Go sets the slice to nil and the collector reclaims in about 4 ms, because the objects are in one heap and the collector can sweep them in bulk. That 4 ms does not include the collector work that ran during the allocation loop and on another thread, so the comparison flatters Go if it is read alone.
Where each language wins
C++ wins where the code must control the machine: a tight loop the compiler can vectorise, a data structure whose layout is chosen by hand, SIMD intrinsics, a memory pool, an allocator that never calls malloc. It also wins when a program must not pause, because there is no collector to pause it.
Go wins where the work per byte is small and the number of concurrent tasks is large: an HTTP server that spends most of its time waiting for a socket, a proxy, a queue consumer. There the cost of a request is dominated by the network, and the language with the cheaper concurrency and the shorter build wins by having more time left for the parts of the program that are not a loop over an array.
Neither of these numbers proves a language faster. They show the size of the constant factor when the algorithms are equal: a few tens of percent, not a few hundred. A program that is ten times slower is almost never slower because of the language; it is slower because of the algorithm, the number of allocations or a lock in the wrong place. The lessons on benchmarks and escape analysis show how to find those.
The previous page of this group is binary size and startup; the next one is refactoring.
The C++ program was built with g++ 15.2.0 (Alpine 15.2.0) at -O2 and the Go program with go 1.26.10, both run on Alpine Linux 3.24.3 (x86_64) in a Docker container on an Intel Core i3-6006U at 2.00 GHz with four hardware threads.
Article author: Arthur Isaev