Skip to content

Instantly share code, notes, and snippets.

@mattn
Created July 19, 2011 07:26
Show Gist options
  • Select an option

  • Save mattn/1091576 to your computer and use it in GitHub Desktop.

Select an option

Save mattn/1091576 to your computer and use it in GitHub Desktop.
// go code
package main
const LEN = 100000
type bench interface {
get() int
}
type B struct {
bench
}
func (b *B) get() int {
return 1
}
type C struct {
bench
}
func (c *C) get() int {
return 2
}
func main() {
//a := new(B)
// meaning get() of B
f := func() int {
return 1
}
sum := 0
for i := 0; i < LEN; i++ {
sum2 := 0
for j := 0; j < LEN; j++ {
sum2 += f()
}
sum += sum2
}
println(sum)
}
// go code
package main
const LEN = 100000
type bench interface {
get() int
}
type B struct {
bench
}
func (b *B) get() int {
return 1
}
type C struct {
bench
}
func (c *C) get() int {
return 2
}
func main() {
//a := new(B)
// meaning get() of B
f := func() int {
return 1
}
sum := 0
for i := 0; i < LEN; i++ {
sum2 := 0
for j := 0; j < LEN; j++ {
sum2 += f()
}
sum += sum2
}
println(sum)
}
// go code
package main
const LEN = 100000
type bench interface {
get() int
}
type B struct {
bench
}
func (b *B) get() int {
return 1
}
type C struct {
bench
}
func (c *C) get() int {
return 2
}
func main() {
sum := 0
for i := 0; i < LEN; i++ {
sum2 := 0
for j := 0; j < LEN; j++ {
sum2 += 1
}
sum += sum2
}
println(sum)
}
// C code:
#include <stdio.h>
#define LEN 100000
struct bench {
virtual int get() = 0;
int test( ) {
int sum=0;
for( int i=0; i<LEN; i++ )
sum += get();
return sum;
}
};
class B : public bench { int get() { return 1; } };
class C : public bench { int get() { return 2; } };
int main() {
bench *f = new B();
int sum=0;
for( int i=0; i<LEN; i++ )
sum += f->test();
printf("%d\n",sum);
}
// go code
package main
const LEN = 100000
type bench interface {
get() int
}
type B struct {
bench
}
func (b *B) get() int {
return 1
}
type C struct {
bench
}
func (c *C) get() int {
return 2
}
func test(a bench) int {
sum := 0
for i := 0; i < LEN; i++ {
sum += a.get()
}
return sum
}
func main() {
var a bench
a = new(B)
sum := 0
for i := 0; i < LEN; i++ {
sum += test(a)
}
println(sum)
}
// Java code:
class B extends bench { int get() { return 1; } }
class C extends bench { int get() { return 2; } }
abstract public class bench {
abstract int get();
static final int LEN=100000;
public static void main(String args[]) {
bench f = new B();
int sum=0;
for( int i=0; i<LEN; i++ )
sum += f.test();
System.out.println(sum);
}
int test( ) {
int sum=0;
for( int i=0; i<LEN; i++ )
sum += get();
return sum;
}
}
*java-normal
real 78.27
user 73.95
sys 0.66
*java-server
real 1.19
user 0.13
sys 0.15
*cpp-O0
real 39.03
user 37.55
sys 0.06
*cpp-O1
real 31.82
user 30.63
sys 0.04
*cpp-O1g
real 24.99
user 23.93
sys 0.07
*cpp-O2
real 29.24
user 28.06
sys 0.06
*cpp-O3
real 27.30
user 26.11
sys 0.05
*golang
real 38.88
user 37.33
sys 0.06
*golang-opt1
real 50.64
user 48.72
sys 0.06
*golang-opt2
real 7.85
user 7.50
sys 0.02
PROG := \
bench.class \
bench-cxx0 \
bench-cxx1 \
bench-cxx1g \
bench-cxx2 \
bench-cxx3 \
bench-go \
bench-go-opt1 \
bench-go-opt2 \
all : $(PROG) benchmark
bench.class : bench.java
javac bench.java
bench-cxx0 : bench.cxx
g++ -O0 -o bench-cxx0 bench.cxx
bench-cxx1 : bench.cxx
g++ -O1 -o bench-cxx1 bench.cxx
bench-cxx1g : bench.cxx
g++ -g -O1 -o bench-cxx1g bench.cxx
bench-cxx2 : bench.cxx
g++ -O2 -o bench-cxx2 bench.cxx
bench-cxx3 : bench.cxx
g++ -O2 -o bench-cxx3 bench.cxx
bench-cxx3fff : bench.cxx
g++ -o bench-cxx3fff -O9 -ftree-vectorize -finline -finline-functions -fomit-frame-pointer -funroll-all-loops bench.cxx
bench-go : bench.go
8g bench.go
8l -o bench-go bench.8
bench-go-opt1 : bench-opt1.go
8g bench-opt1.go
8l -o bench-go-opt1 bench-opt1.8
bench-go-opt2 : bench-opt2.go
8g bench-opt2.go
8l -o bench-go-opt2 bench-opt2.8
benchmark :
rm -f benchmark.log
echo "*java-normal" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p java bench
echo >> benchmark.log
echo "*java-server" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p java -server bench
echo >> benchmark.log
echo "*cpp-O0" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-cxx0
echo >> benchmark.log
echo "*cpp-O1" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-cxx1
echo >> benchmark.log
echo "*cpp-O1g" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-cxx1g
echo >> benchmark.log
echo "*cpp-O2" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-cxx2
echo >> benchmark.log
echo "*cpp-O3" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-cxx3
echo >> benchmark.log
echo "*golang" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-go
echo >> benchmark.log
echo "*golang-opt1" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-go-opt1
echo >> benchmark.log
echo "*golang-opt2" >> benchmark.log
/usr/bin/time -o benchmark.log -a -p ./bench-go-opt2
echo >> benchmark.log
clean :
rm benchmark.log bench-cxx* bench-go *.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment