x64 ArchLinux : Intel® i5-7200U®

Each chart bar shows *how many times slower*, one ↓ **fannkuch-redux** program was, compared to the fastest program.

These are not the only programs that could be written. These are not the only compilers and interpreters. These are not the only programming languages.

Column × shows *how many times more* each program used compared to the benchmark program that used least.

sort | sort | sort | ||||

× | Program Source Code | CPU secs | Elapsed secs | Memory KB | Code B | ≈ CPU Load |
---|---|---|---|---|---|---|

1.0 | PyPy 2 |
3.09 | 1.09 | 81,348 | 1009 | 94% 66% 72% 69% |

1.1 | PyPy 3 |
3.40 | 1.25 | 80,504 | 1271 | 97% 63% 64% 60% |

1.2 | PyPy 3 #2 |
3.28 | 1.32 | 79,976 | 1008 | 59% 95% 53% 54% |

1.2 | PyPy 3 #6 |
1.28 | 1.35 | 67,552 | 552 | 99% 3% 2% 1% |

1.4 | PyPy 3 #3 |
4.12 | 1.53 | 80,448 | 894 | 64% 93% 59% 68% |

1.4 | PyPy 3 #4 |
4.36 | 1.53 | 79,376 | 1069 | 72% 66% 92% 70% |

2.5 | Nuitka #3 |
10.12 | 2.69 | 13,624 | 894 | 98% 93% 98% 94% |

2.6 | Nuitka #4 |
10.55 | 2.81 | 13,496 | 1069 | 100% 95% 96% 94% |

2.8 | Pyston |
11.20 | 3.07 | 31,188 | 1009 | 94% 96% 92% 93% |

2.8 | Python development version #3 |
11.66 | 3.11 | 12,624 | 894 | 96% 98% 91% 98% |

2.9 | Python 3 #3 |
11.99 | 3.19 | 12,420 | 894 | 98% 94% 96% 96% |

2.9 | Nuitka #2 |
12.19 | 3.19 | 13,552 | 1008 | 100% 97% 95% 97% |

2.9 | Graal #6 |
3.34 | 3.20 | 464,152 | 552 | 96% 5% 2% 10% |

3.1 | Python development version #4 |
12.54 | 3.33 | 12,700 | 1069 | 99% 94% 97% 96% |

3.1 | Python 3 #4 |
12.66 | 3.43 | 12,776 | 1069 | 98% 92% 96% 93% |

3.6 | Nuitka |
14.89 | 3.93 | 13,564 | 1271 | 99% 96% 96% 96% |

3.7 | Nuitka #6 |
3.91 | 4.09 | 9,412 | 552 | 100% 2% 1% 1% |

3.8 | Python 3 #2 |
15.76 | 4.19 | 12,516 | 1008 | 99% 95% 96% 94% |

3.9 | Python development version #2 |
16.34 | 4.22 | 12,756 | 1008 | 99% 99% 99% 99% |

4.1 | Python 2 |
16.77 | 4.44 | 11,648 | 1009 | 99% 97% 94% 96% |

4.5 | Python development version |
18.76 | 4.86 | 12,908 | 1271 | 99% 98% 98% 99% |

4.7 | Python 3 |
19.50 | 5.14 | 12,720 | 1271 | 99% 98% 95% 96% |

5.0 | Python 3 #6 |
5.44 | 5.45 | 8,864 | 552 | 6% 2% 100% 2% |

5.1 | Python development version #6 |
5.37 | 5.61 | 7,512 | 552 | 100% 1% 1% 1% |

7.9 | MicroPython #6 |
8.59 | 8.60 | 4,096 | 552 | 6% 2% 2% 100% |

missing benchmark programs | ||||||

Jython |
No program | |||||

IronPython |
No program | |||||

Cython |
No program | |||||

Shedskin |
No program | |||||

Numba |
No program | |||||

Grumpy |
No program | |||||

RustPython |
No program |

**diff** program output N = 7 with this output file to check your program is correct before contributing.

We are trying to show the performance of various programming language implementations - so we ask that contributed programs not only give the correct result, but also **use the same algorithm** to calculate that result.

For N = 7 programs should generate these permutations (40KB) - which, incidentally, seem to be in the same order as permutations generated by the Tompkins-Paige algorithm, see pages 150-151 Permutation Generation Methods Robert Sedgewick.

The fannkuch benchmark is defined by programs in Performing Lisp Analysis of the FANNKUCH Benchmark, Kenneth R. Anderson and Duane Rettig.

Each program should

- Take a permutation of {1,...,n}, for example: {4,2,1,5,3}.
- Take the first element, here 4, and reverse the order of the first 4 elements: {5,1,2,4,3}.
- Repeat this until the first element is a 1, so flipping won't change anything more: {3,4,2,1,5}, {2,4,3,1,5}, {4,2,3,1,5}, {1,3,2,4,5}.
- Count the number of flips, here 5.
- Keep a checksum
- checksum = checksum + (if permutation_index is even then flips_count else -flips_count)
- checksum = checksum + (toggle_sign_-1_1 * flips_count)

- Do this for all n! permutations, and record the maximum number of flips needed for any permutation.

The conjecture is that this maximum count is approximated by n*log(n) when n goes to infinity.

*FANNKUCH* is an abbreviation for the German word *Pfannkuchen*, or pancakes, in analogy to flipping pancakes.

Thanks to Oleg Mazurov for insisting on a checksum and providing this helpful description of the approach he took -

- A common idea for parallel implementation is to divide all work (n! permutations) into chunks small enough to avoid load imbalance but large enough to keep overhead low. I set the number of chunks as a parameter (NCHUNKS = 150) from which I derive the size of a chunk (CHUNKSZ) and the actual number of chunks/tasks to be processed (NTASKS), which may be different from NCHUNKS because of rounding.
- Task scheduling is trivial: threads will atomically get and increment the taskId variable to derive a range of permutation indices to work on:
task = taskId.getAndIncrement(); idxMin = task * CHUNKSZ; idxMax = min( idxMin + CHUNKSZ, n! );

- Maximum flip counts and partial checksums can be computed for chunks in arbitrary order and recombined to generate the required result at the final step (CHUNKSZ must be even for adding partial checksums to be associative - I didn't enforce it in my submission).
- Now I need to go from a permutation index to the permutation itself.
- The predefined order in which all permutations are to be generated can be described as follows: to generate n! permutations of n arbitrary numbers, rotate the numbers left (from higher position to lower) n times, so that each number appears in the n-th position, and for each rotation recursively generate (n-1)! permutations of the first n-1 numbers whatever they are.
- To optimize the process I use an intermediate data structure, count[], which keeps count of how many rotations have been done at every level. Apparently, count[0] is always 0, as there is only one element at that level, which can't be rotated; count[1] = 0..1 for two elements, count[2] = 0..2 for three elements, etc.
- To generate next permutation I swap the first two elements and increase count[1]. If count[1] becomes greater than 1, I'm done with rotations at level 1 and need to "return" (as it would have been in the recursive implementation) to level 2. Now, I rotate 3 elements and increment count[2]. If it becomes greater than 2, I'm done with level 2 and need to go to level 3, etc.
- It should be clear now how to generate a permutation and corresponding count[] array from an arbitrary index. Basically, count[k] = ( index % (k+1)! ) / k! is the number of rotations we need to perform on elements 0..k. Doing it in the descending order from n-1 to 1 gives us both the count[] array and the permutation.