Benchmarks
To compare the speed of all Linux computers I get my fingers on, I use a simple program to calculate n! (n<120000). The program was compiled with gcc, optimization enabled (-O2). Here are the results:
|
|
Devices written in bold are still in use
/*********************************************************************/
unsigned int f[200000];
#include <stdio.h>
#include <time.h>
void compute(unsigned int n, unsigned int *f)
{unsigned int i=1,j,k,l,m=1; f[1]=1;
for (j=2; j<=n; j++)
{l=0; for (k=m; k<=i; k++)
{l=j*f[k]+l; f[k]=l&0x7FFF; l=l>>15;}
if (l) {f[++i]=l&0x7FFF;
if (l>>15) f[++i]=l>>15;}
if (!f[m]) m++;}
*f=i;}
void print_result(unsigned int n,unsigned int *f)
{unsigned int i=*f,j,k,l=0,m=(i-1)%4+15;
while(i) {j=0; for (k=0;k<4;k++) {j=(j<<1)+((f[i]>>m)&1);if(!m--){m=14;i--;}}
if (!(l&0x3F)) putchar('\n'); putchar(j<10?j+'0':j-10+'A'); l++;}
printf("\n\n%d! has %d hex digits\n",n,l);}
int main() /* int must be >= 4 byte */
{ unsigned int i, n;
do {
printf("calculate n! (n<=120000) n=? ");
i= scanf("%d",&n);
} while (n>120000 );
clock_t start = clock(); compute(n,f); clock_t end = clock();
/*print_result(n,f);*/
printf("time = %ld ms\n", (end-start) / (CLOCKS_PER_SEC / 1000));
}
/*********************************************************************/
C is outdated and not recommended for new projects. 🙂️ Use Rust instead. 🥰️ Below version is optimized.
Safe code as bench.rs and compile with "rustc -O bench.rs"
use std::io;
use std::time::{Instant};
fn compute(n: usize, f: &mut [u32]) -> usize {
let mut i = 1;
let mut l = 0u64;
let mut m = 1;
f[1] = 1;
for j in 2..=n {
for k in m..=i {
l += j as u64 * f[k] as u64;
f[k] = l as u32;
l >>= 32;
}
if l != 0 {
i += 1;
f[i] = l as u32;
if l >> 32 != 0 {
i += 1;
f[i] = (l >> 32) as u32;
}
}
if f[m] == 0 {
m += 1;
}
}
i
}
fn main() {
let mut f = [0u32; 60_000];
let n = loop {
println!("calculate n! (n<=120000) n=?");
let mut n = String::new();
io::stdin()
.read_line(&mut n)
.expect("Failed to read line");
let n = match n.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
if n <= 120000 {
break n;
}
};
let start = Instant::now();
println!("elemente: {}", compute( n, &mut f));
let elapsed = start.elapsed();
println!("n! von {n} in {} ms", elapsed.as_millis());
}
page revision: 63, last edited: 11 Dec 2024 21:22