[New-bugs-announce] [issue29698] _collectionsmodule.c: Replace `n++; while (--n)` with `for (; n; --n)`
Nikita Kniazev
report at bugs.python.org
Thu Mar 2 11:46:33 EST 2017
New submission from Nikita Kniazev:
I have failed to find previous discussion when `while (n--)` was changed to `n++; while (--n)`. (commits 306d6b1ea6bf2582b9284be2fd27275abbade3e1, 165eee214bc388eb588db33385ca49ddbb305565)
It is clear for me that n++; while (--n) and for (; n; --n) are interchangable statements, but here is the prof of it http://coliru.stacked-crooked.com/a/a6fc4108b223e7b2.
According to asm out https://godbolt.org/g/heHM33 the `for` loop is even shorter (takes less instructions).
While I believe that location of `cmp`/`jmp` instruction makes no sense and performance is the same, but I have made a benchmark.
```
Run on (4 X 3310 MHz CPU s)
02/27/17 22:10:55
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_while_loop/2 13 ns 13 ns 56089384
BM_while_loop/4 17 ns 16 ns 40792279
BM_while_loop/8 24 ns 24 ns 29914338
BM_while_loop/16 40 ns 40 ns 20396140
BM_while_loop/32 84 ns 80 ns 8974301
BM_while_loop/64 146 ns 146 ns 4487151
BM_while_loop/128 270 ns 269 ns 2492862
BM_while_loop/128 267 ns 266 ns 2639500
BM_while_loop/512 1022 ns 1022 ns 641022
BM_while_loop/4096 8203 ns 8344 ns 89743
BM_while_loop/32768 66971 ns 66750 ns 11218
BM_while_loop/262144 545833 ns 546003 ns 1000
BM_while_loop/2097152 4376095 ns 4387528 ns 160
BM_while_loop/8388608 17654654 ns 17883041 ns 41
BM_for_loop/2 13 ns 13 ns 56089384
BM_for_loop/4 15 ns 15 ns 49857230
BM_for_loop/8 21 ns 21 ns 32051077
BM_for_loop/16 37 ns 37 ns 19509351
BM_for_loop/32 81 ns 80 ns 8974301
BM_for_loop/64 144 ns 128 ns 4985723
BM_for_loop/128 265 ns 263 ns 3205108
BM_for_loop/128 265 ns 266 ns 2639500
BM_for_loop/512 1036 ns 1022 ns 641022
BM_for_loop/4096 8314 ns 8344 ns 89743
BM_for_loop/32768 67345 ns 66750 ns 11218
BM_for_loop/262144 541310 ns 546004 ns 1000
BM_for_loop/2097152 4354986 ns 4387528 ns 160
BM_for_loop/8388608 17592428 ns 17122061 ns 41
```
```cpp
#include <benchmark/benchmark.h>
#define MAKE_ROTL_BENCHMARK(name) \
static void BM_##name(benchmark::State& state) { \
while (state.KeepRunning()) { \
int n = name(state.range(0)); \
} \
} \
/**/
int while_loop(int n)
{
int sum = 0;
n++;
while (--n) {
sum += 1;
}
return sum;
}
int for_loop(int n)
{
int sum = 0;
for(; n; --n) {
sum += 1;
}
return sum;
}
MAKE_ROTL_BENCHMARK(while_loop)
MAKE_ROTL_BENCHMARK(for_loop)
BENCHMARK(BM_while_loop)->RangeMultiplier(2)->Range(2, 8<<4);
BENCHMARK(BM_while_loop)->Range(8<<4, 8<<20);
BENCHMARK(BM_for_loop)->RangeMultiplier(2)->Range(2, 8<<4);
BENCHMARK(BM_for_loop)->Range(8<<4, 8<<20);
BENCHMARK_MAIN()
```
----------
components: Interpreter Core
messages: 288815
nosy: Kojoley
priority: normal
pull_requests: 329
severity: normal
status: open
title: _collectionsmodule.c: Replace `n++; while (--n)` with `for (; n; --n)`
type: enhancement
versions: Python 3.6, Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29698>
_______________________________________
More information about the New-bugs-announce
mailing list