Skip to content

Instantly share code, notes, and snippets.

@mpatraw
Forked from tommyettinger/Linnorm.java
Last active May 16, 2018 21:03
Show Gist options
  • Save mpatraw/fbeb705c39c4f001e5850577137afdf4 to your computer and use it in GitHub Desktop.
Save mpatraw/fbeb705c39c4f001e5850577137afdf4 to your computer and use it in GitHub Desktop.
Linnorm benchmarks
public final class Linnorm {
private static long state = 0L;
private static long random()
{
long z = (state = state * 0x41C64E6DL + 1L);
z = (z ^ z >>> 28) * 0xAEF17502108EF2D9L;
return z ^ z >>> 30;
}
public static void main(String[] args) {
long out = 0L;
for (int i = 0; i < 1000000000; i++) {
out += random(); // Java is inlining this; manually inlining slows it down
}
final long startTime = System.nanoTime();
for (int a = 0; a < 20; a++) {
for (int i = 0; i < 1000000000; i++) {
out += random();
}
}
System.out.println(System.nanoTime() - startTime);
System.out.println(out); // may be negative, Rust uses an unsigned type so convert before checking equality
}
}
extern crate time;
use std::num::Wrapping as W;
#[inline]
fn linnorm(state: &mut W<u64>) -> W<u64> {
*state = *state * W(0x41C64E6Du64) + W(1u64);
let mut z = *state;
z = (z ^ z >> 28) * W(0xAEF17502108EF2D9u64);
z ^ z >> 30
}
fn main() {
let mut state: W<u64> = W(0u64);
let mut out: W<u64> = W(0u64);
let t = time::precise_time_ns();
for _ in 0..1_000_000_000 {
out += linnorm(&mut state);
}
for _ in 0..20 {
for _ in 0..1_000_000_000 {
out += linnorm(&mut state);
}
}
println!("{}", time::precise_time_ns() - t);
println!("{}", out.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment