Rust 実行速度

use std::os::raw::c_int;

#[link(name="mylib", kind="static")]
extern "C" {
	fn twice(n: c_int) -> c_int;
}

fn main() {
	let n = 12;
	unsafe {
		println!("{}の2倍は{}", n, twice(n));
	}
}
use std::time::Instant;

fun main() {
	i16func();
	i64func();
}

fn i16func(){
	let mut v:16 = 0;
	let start = Instant::now();
	for _ in 0..1000 {
		for _ in 0..10000 {
			v = v + 1;
		}
		v = v - 10000;
	}
	let elepse = start.elapsed();
	println!("i16={:?}", v);
}

fn i64func() {
	let mut v:i64 = 0;
	let start = Instant::now();
	for _ in 0..1000 {
		for _ in 0..10000 {
			v = v + 1;
		}
		v = v- 10000;
	}
	let elepse = start.elapsed();
	println!("i64={:?}", v);
	println!("{}ミリ秒", elepse.as_millis());
}
use std::time::Instant;

fn main() {
	f32func();
	f64func();
}

fn f32func() {
	const NDATA:usize = 10000;
	let mut a:[f32; NDATA] = [0.0; NDATA];
	for i in 0..NDATA {
		a[i] = (NDATA - i) as f32;
	}
	println!("並び替えスタート");
	let start = Instant::now();
	for i in 0..NDATA{
		for j in 0..NDATA {
			if a[i] < a[j] {
				let tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
	let elepse = start.elapsed();
	println!("並べ替え終了");
	println!("{}ミリ秒", elepse.as_millis());
}

fn f64func() {
	const NDATA:usize = 10000;
	let mut a:[f64; NDATA] = [0.0; NDATA];
	for i in 0..NDATA {
		a[i] = (NDATA - i) as f64;
	}
	let start = Instant::now();
	for i in 0..NDATA {
		for j in 0..NDATA {
			if a[i] < a[j] {
				let tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
	let elepse = start.elapsed();
	println!("並び替え終了");
	println!("{}ミリ秒", elepse.as_millis());
}