Parse integers Sergey Svistunov

Compute the sum of integers read from STDIN as fast as possible.

Input: 50 000 000 lines, each containing one integer in the range [0; 2147483647].

629871117
2024562523
1372689083
1021777120
2111176472

Output: The uint64 sum of all numbers, printed as a decimal string.

Note: Integer overflow is expected – use a 64-bit accumulator.

Discussion (7)
Log in to join the discussion.
wn
wnAug 11, 2026 16:21

I have a lot of cool stuff in my code, like loop unrolling, fancy compiler flags, prefetching, etc… and i believe those contributes to a faster program even though it performs more complex operations. I also ensure that my code has minimal data dependencies (your acc is used by va and vb).

Most of your program time is spent waiting for data (memory bound). Mine too, but I do my CPU computation stuff at the meantime, so just because my program is doing more stuff doesn’t mean i automatically need more time.

Tony
TonyAug 10, 2026 20:49

Oopps sorry I pasted the table, I didn’t realise it would render like this.

Tony
TonyAug 10, 2026 20:48

Your run is 

#

Date

Score

Wall Time

CPU User

CPU System

Memory

Error

 

1

Jul 7, 2026 05:29

3,292

35,250,562

26,294,000

1,035,000

2,113,536

 

 

2

Jul 7, 2026 05:29

3,292

35,139,783

26,326,000

1,003,000

2,113,536

 

 

3

Jul 7, 2026 05:29

3,292

35,162,866

26,313,000

1,016,000

2,113,536

 

 

4

Jul 7, 2026 05:29

3,293

35,359,055

26,309,000

1,027,000

2,113,536

 

 

5

Jul 7, 2026 05:29

3,294

35,177,755

26,342,000

1,005,000

2,113,536

 

 

6

Jul 7, 2026 05:29

3,304

35,254,439

26,410,000

1,015,000

2,113,536

 

 

7

Jul 7, 2026 05:29

3,402

36,100,457

27,237,000

1,007,000

2,113,536

 

 

8

Jul 7, 2026 05:29

3,466

36,635,521

27,765,000

1,011,000

2,113,536

 

 

9

Jul 7, 2026 05:29

3,473

37,323,884

27,795,000

1,032,000

2,113,536

 

if I run the code below, I get a CPU User slower than yours, this is without trying to answer the solution itself.

Run Statistics

#

Date

Score

Wall Time

CPU User

CPU System

Memory

Error

 

1

Aug 10, 2026 20:47

0

39,068,386

28,128,000

1,071,000

2,113,536

expected “53684444064224082 “, got “50000000 “

 

2

Aug 10, 2026 20:47

0

39,068,373

27,993,000

1,091,000

2,113,536

expected “53691875862706076 “, got “50000000 “

 

3

Aug 10, 2026 20:47

0

40,809,203

29,874,000

1,090,000

2,113,536

expected “53693074276993102 “, got “50000000 “

wn
wnAug 10, 2026 05:12

“goes through the data is somehow slower.”

What are you comparing slower with? My solution?

Tony
TonyAug 9, 2026 18:03

What I mean is that even this trivial version that does not answer the problem but goes through the data is somehow slower. So I feel like I have missed something.


//   g++ -O3 -march=native 

#include <immintrin.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <cstdint>
#include <cstdio>

static constexpr size_t kTurn = 512;

int main() {
    size_t size = syscall(SYS_lseek, 0, 0, 2 /*SEEK_END*/) - 1;

    char const* start = (char const*)syscall(SYS_mmap, 0, size, PROT_READ,
                                             MAP_PRIVATE | MAP_POPULATE,
                                             STDIN_FILENO, 0);

    size_t const body = size & ~(kTurn - 1);

    size_t const half  = (body / 2) & ~(kTurn - 1);
    size_t const turns = half / kTurn;

    __m256i const nl = _mm256_set1_epi8('\n');
    uint64_t acc = 0;

    char const* a = start;
    char const* b = start + half;

    for (size_t t = 0; t < turns; ++t, a += kTurn, b += kTurn) {
        for (size_t k = 0; k < kTurn; k += 32) {
            __m256i va = _mm256_loadu_si256((__m256i const*)(a + k));
            __m256i vb = _mm256_loadu_si256((__m256i const*)(b + k));
            acc += _mm_popcnt_u32((uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(va, nl)));
            acc += _mm_popcnt_u32((uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(vb, nl)));
        }
    }

    char const* p   = b;
    char const* end = start + size;
    for (; p + 32 <= end; p += 32) {
        __m256i v = _mm256_loadu_si256((__m256i const*)p);
        acc += _mm_popcnt_u32((uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, nl)));
    }
    for (; p < end; ++p) acc += (*p == '\n');

    printf("%llu\n", (unsigned long long)(acc + 1));  
    return 0;
}

wn
wnAug 9, 2026 08:23
    // Get offset to end of file. -1 to remove trailing '\0'
    size_t offset = syscall(SYS_lseek, 0, 0, 2 /*SEEK_END*/) - 1;

    char const* start =
        (char const*)syscall(SYS_mmap, 0, offset, PROT_READ,
                             MAP_PRIVATE | MAP_POPULATE, STDIN_FILENO, 0);

mmap isnt reading anything. its just mapping the file from the physical page to your program virtual memory.

But this is how I “read” with mmap.

1
Tony
TonyAug 8, 2026 20:26

Is there a trick to read as fast as the people in the top? I think even if I just mmap read, I am not as fast as the #1?