Skip to content

Instantly share code, notes, and snippets.

@hktonylee
Last active August 29, 2015 14:02
Show Gist options
  • Save hktonylee/89f937510c0f92e3f1c5 to your computer and use it in GitHub Desktop.
Save hktonylee/89f937510c0f92e3f1c5 to your computer and use it in GitHub Desktop.
Simple Performance Comparison Between Swift, C and Java
test1.swift
==========================================
Run #1
------
real 0m0.356s
user 0m0.352s
sys 0m0.003s
Run #2
------
real 0m0.342s
user 0m0.336s
sys 0m0.005s
Run #3
------
real 0m0.353s
user 0m0.348s
sys 0m0.003s
Run #4
------
real 0m0.355s
user 0m0.351s
sys 0m0.003s
Run #5
------
real 0m0.356s
user 0m0.352s
sys 0m0.003s
Run #6
------
real 0m0.355s
user 0m0.347s
sys 0m0.006s
test1.c
==========================================
Run #1
------
real 0m0.110s
user 0m0.107s
sys 0m0.001s
Run #2
------
real 0m0.109s
user 0m0.106s
sys 0m0.002s
Run #3
------
real 0m0.102s
user 0m0.099s
sys 0m0.001s
Run #4
------
real 0m0.108s
user 0m0.105s
sys 0m0.001s
Run #5
------
real 0m0.107s
user 0m0.105s
sys 0m0.001s
Run #6
------
real 0m0.107s
user 0m0.104s
sys 0m0.002s
test1.java
==========================================
Run #1
------
real 0m0.400s
user 0m0.375s
sys 0m0.027s
Run #2
------
real 0m0.399s
user 0m0.372s
sys 0m0.027s
Run #3
------
real 0m0.395s
user 0m0.372s
sys 0m0.025s
Run #4
------
real 0m0.397s
user 0m0.372s
sys 0m0.027s
Run #5
------
real 0m0.390s
user 0m0.369s
sys 0m0.026s
Run #6
------
real 0m0.393s
user 0m0.371s
sys 0m0.025s
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
const int N = 10000000;
const int M = 10000000;
int count = 0;
for (int i = 0; i < N; i++) {
double x = (double)(random() % (M + 1)) / (double)(M);
double y = (double)(random() % (M + 1)) / (double)(M);
if (x * x + y * y <= 1) {
count++;
}
}
double pi_approx = 4.0 * (double)(count) / (double)(N);
printf("pi .= %f", pi_approx);
return 0;
}
import java.util.Random;
public class test1 {
public static void main(String[] args) {
final Random rand = new Random();
final int N = 10000000;
final int M = 10000000;
int count = 0;
for (int i = 0; i < N; i++) {
final double x = (double)(rand.nextInt() % (M + 1)) / M;
final double y = (double)(rand.nextInt() % (M + 1)) / M;
if (x * x + y * y <= 1) {
count++;
}
}
final double pi_approx = 4.0 * (double)(count) / (double)(N);
System.out.format("pi .= %f%n", pi_approx);
}
}
import Foundation
let N = 10_000_000
let M = 10_000_000
var count = 0
for i in 0..N {
let x = Double(random() % (M + 1)) / Double(M)
let y = Double(random() % (M + 1)) / Double(M)
if x * x + y * y <= 1 {
count++
}
}
let pi_approx = 4.0 * Double(count) / Double(N)
println("pi .= \(pi_approx)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment