GitLab will be upgraded to the 12.10.14-ce.0 on 28 Sept 2020 at 2.00pm (AEDT) to 2.30pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

Commit de11807c authored by John Zhang's avatar John Zhang

fannkuch reduction benchmark from benchmark shootout game

parent d7f90a7b
fannkuchredux:
benchmark:
name: game/fannkuchredux
args:
- 10
iterations: 10
callback: clock
environ:
PYPY_USESSION_DIR: example
tasks:
rpyc:
language:
name: rpython
backend: c
source: targetfannkuchredux.py
environ:
PYPY_USESSION_BASENAME: "${MUBENCH_TASKSET_NAME}_${MUBENCH_TASK_NAME}"
rpyzebu:
language:
name: rpython
backend: mu
impl: zebu
source: targetfannkuchredux.py
environ:
PYPY_USESSION_BASENAME: "${MUBENCH_TASKSET_NAME}_${MUBENCH_TASK_NAME}"
c_O3:
language: c
source: fannkuchredux.c
compiler:
flags:
- -O3
\ No newline at end of file
/* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* converted to C by Joseph Piché
* from Java version by Oleg Mazurov and Isaac Gouy
*
* Modified by John Zhang
*/
#include <stdio.h>
#include <stdlib.h>
#include "callback.h"
int fannkuchredux(int n)
{
int perm[n];
int perm1[n];
int count[n];
int maxFlipsCount = 0;
int permCount = 0;
int i;
for (i=0; i<n; i+=1)
perm1[i] = i;
int r = n;
while (1) {
while (r != 1) {
count[r-1] = r;
r -= 1;
}
for (i=0; i<n; i+=1)
perm[i] = perm1[i];
int flipsCount = 0;
int k;
while ( !((k = perm[0]) == 0) ) {
int k2 = (k+1) >> 1;
for (i=0; i<k2; i++) {
int temp = perm[i]; perm[i] = perm[k-i]; perm[k-i] = temp;
}
flipsCount += 1;
}
maxFlipsCount = (maxFlipsCount > flipsCount) ? maxFlipsCount : flipsCount;
/* Use incremental change to generate another permutation */
while (1) {
if (r == n) {
return maxFlipsCount;
}
int perm0 = perm1[0];
i = 0;
while (i < r) {
int j = i + 1;
perm1[i] = perm1[j];
i = j;
}
perm1[r] = perm0;
count[r] = count[r] - 1;
if (count[r] > 0) break;
r++;
}
permCount++;
}
}
int main(int argc, char *argv[])
{
struct Callback* cb;
int n, res;
cb = cb_init(argv[1]);
n = argc > 2 ? atoi(argv[2]) : 7;
cb_begin(cb, NULL);
res = fannkuchredux(n);
cb_end(cb, NULL);
printf("fannkuchredux(%d) = %d\n", n, res);
cb_report(cb);
return 0;
}
# Adaptation of the C version in fannkuchredux.c
# author: John Zhang
def fannkuchredux(n):
perm = [0] * n
perm1 = [i for i in range(n)]
count = [0] * n
maxFlipsCount = 0
permCount = 0
r = n
while True:
while r != 1:
count[r-1] = r
r -= 1
for i in range(n):
perm[i] = perm1[i]
flipsCount = 0
k = perm[0]
while k != 0:
k2 = (k + 1) >> 1
for i in range(k2):
temp = perm[i]
perm[i] = perm[k-i]
perm[k-i] = temp
flipsCount += 1
k = perm[0]
maxFlipsCount = maxFlipsCount if (maxFlipsCount > flipsCount) else flipsCount
while True:
if r == n:
return maxFlipsCount
perm0 = perm1[0]
i = 0
while i < r:
j = i + 1
perm1[i] = perm1[j]
i = j
perm1[r] = perm0
count[r] = count[r] - 1
if count[r] > 0:
break
r += 1
permCount += 1
def target(driver, args):
from callback import get_callback
cb_cls = get_callback(args[0])
def main(argv):
cb_param = argv[1]
n = int(argv[2]) if len(argv) > 2 else 7
cb = cb_cls(cb_param)
cb.begin()
res = fannkuchredux(n)
cb.end()
print "fannkuchredux(%d) = %d" % (n, res)
cb.report()
return 0
return main, None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment