Last active
February 19, 2018 10:36
-
-
Save qRoC/2165323308c9cf62bb41 to your computer and use it in GitHub Desktop.
GO compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#include <float.h> | |
#include <sys/time.h> | |
#include <stdint.h> | |
typedef int64_t TimeVal; | |
TimeVal getPerfTime() | |
{ | |
timeval now; | |
gettimeofday(&now, 0); | |
return (TimeVal)now.tv_sec*1000000L + (TimeVal)now.tv_usec; | |
} | |
int getPerfDeltaTimeUsec(const TimeVal start, const TimeVal end) | |
{ | |
return (int)(end - start); | |
} | |
float InvSqrt(float x) | |
{ | |
float xhalf = 0.5f*x; | |
int i = *(int *)&x; | |
i = 0x5f3759df - (i >> 1); | |
x = *(float *)&i; | |
x = x*(1.5f - xhalf*x*x); | |
return x; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
TimeVal timeStart = getPerfTime(); | |
double res = 0.0; | |
for (int z = 0; z < 10000000; ++z) | |
{ | |
res += InvSqrt(double(4121214.151244124125)); | |
} | |
TimeVal timeEnd = getPerfTime(); | |
printf("%f # %.3fms\n", res, getPerfDeltaTimeUsec(timeStart, timeEnd) / 1000.0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"math" | |
) | |
func InvSqrt(x float32) float32 { | |
xhalf := 0.5 * x | |
i := math.Float32bits(x) | |
i = 0x5f3759df - (i >> 1) | |
x = math.Float32frombits(i) | |
x = x * (1.5 - xhalf * x * x) | |
return x | |
} | |
func main() { | |
time_start := time.Now() | |
var res float64 | |
for z := 0; z < 10000000; z++ { | |
res += float64(InvSqrt(4121214.151244124125)) | |
} | |
fmt.Print(res) | |
fmt.Print(" # ") | |
fmt.Println(time.Now().Sub(time_start)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#include <float.h> | |
#include <sys/time.h> | |
#include <stdint.h> | |
typedef int64_t TimeVal; | |
TimeVal getPerfTime() | |
{ | |
timeval now; | |
gettimeofday(&now, 0); | |
return (TimeVal)now.tv_sec*1000000L + (TimeVal)now.tv_usec; | |
} | |
int getPerfDeltaTimeUsec(const TimeVal start, const TimeVal end) | |
{ | |
return (int)(end - start); | |
} | |
float InvSqrt(float x) | |
{ | |
return 1.0 / sqrtf(x); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
TimeVal timeStart = getPerfTime(); | |
double res = 0.0; | |
for (int z = 0; z < 10000000; ++z) | |
{ | |
res += InvSqrt(4121214.151244124125); | |
} | |
TimeVal timeEnd = getPerfTime(); | |
printf("%f # %.3fms\n", res, getPerfDeltaTimeUsec(timeStart, timeEnd) / 1000.0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
//go:noescape | |
func Sqrt(x float32) float32 // ./main2_$GOARCH.s | |
func InvSqrt(x float32) float32 { | |
return float32(1.0 / Sqrt(x)) | |
} | |
func main() { | |
time_start := time.Now() | |
var res float64 | |
for z := 0; z < 10000000; z++ { | |
res += float64(InvSqrt(4121214.151244124125)) | |
} | |
fmt.Print(res) | |
fmt.Print(" # ") | |
fmt.Println(time.Now().Sub(time_start)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// func Sqrt(x float32) float32 | |
TEXT ·Sqrt+0(SB),$0-16 | |
SQRTSS x+0(FP), X0 | |
MOVSS X0,r+8(FP) | |
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
v1 | |
4917.670739814639 # 24.788318ms | |
v2 | |
4925.920511595905 # 50.62856ms | |
v3 | |
4917.670740 # 8.815ms | |
v4 | |
4925.920512 # 12.797ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cd main1; go build -o "v1"; cd .. | |
cd main2; go build -o "v2"; cd .. | |
g++ -O3 -o v3 main.cpp | |
g++ -O3 -o v4 main2.cpp | |
echo | |
echo "v1" | |
./main1/v1 | |
echo | |
echo "v2" | |
./main2/v2 | |
echo | |
echo "v3" | |
./v3 | |
echo | |
echo "v4" | |
./v4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment