Last active
June 23, 2018 14:06
-
-
Save hajimehoshi/f9299abc0890283dfe803ed80767f5b5 to your computer and use it in GitHub Desktop.
GopherJS vs Wasm (2)
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 ( | |
"time" | |
) | |
func array() []float32 { | |
return make([]float32, 16) | |
} | |
func heavyTask() float32 { | |
arr := array() | |
result := float32(0.0) | |
for i := 0; i < 10000000; i++ { | |
u0, v0, u1, v1 := float32(0.0), float32(0.0), float32(1.0), float32(1.0) | |
arr[0] = u0 | |
arr[1] = v0 | |
arr[2] = u1 | |
arr[3] = u1 | |
arr[4] = u1 | |
arr[5] = v0 | |
arr[6] = u0 | |
arr[7] = u1 | |
arr[8] = u0 | |
arr[9] = v1 | |
arr[10] = u1 | |
arr[11] = u0 | |
arr[12] = u1 | |
arr[13] = v1 | |
arr[14] = u0 | |
arr[15] = u0 | |
result += arr[i % 16] | |
} | |
return result | |
} | |
func main() { | |
t1 := time.Now().UnixNano() | |
x := heavyTask() | |
t2 := time.Now().UnixNano() | |
println("result:", x) | |
println("time [ns]:", t2 - t1) | |
} |
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
function array() { | |
return new Float32Array(16); | |
} | |
function heavyTask() { | |
const arr = array(); | |
result = 0.0; | |
for (let i = 0; i < 10000000; i++) { | |
const u0 = 0.0; | |
const v0 = 0.0; | |
const u1 = 1.0; | |
const v1 = 1.0; | |
arr[0] = u0 | |
arr[1] = v0 | |
arr[2] = u1 | |
arr[3] = u1 | |
arr[4] = u1 | |
arr[5] = v0 | |
arr[6] = u0 | |
arr[7] = u1 | |
arr[8] = u0 | |
arr[9] = v1 | |
arr[10] = u1 | |
arr[11] = u0 | |
arr[12] = u1 | |
arr[13] = v1 | |
arr[14] = u0 | |
arr[15] = u0 | |
result += arr[i % 16] | |
} | |
return result; | |
} | |
function main() { | |
const t1 = process.hrtime(); | |
const x = heavyTask(); | |
const diff = process.hrtime(t1); | |
console.log('result:', x); | |
console.log('time [ns]:', diff[0] * 1e9 + diff[1]); | |
} |
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
Go | |
result: +5.000000e+006 | |
time [ns]: 47944663 | |
Wasm (Go) | |
result: +5.000000e+006 | |
time [ns]: 228000000 | |
GopherJS | |
result: 5000000 | |
time [ns]: 169000000 | |
JavaScript | |
result: 5000000 | |
time [ns]: 77574471 |
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
echo 'Go' | |
go run main.go | |
echo | |
echo 'Wasm (Go)' | |
GOOS=js GOARCH=wasm ~/go-code/bin/go build -o test.wasm . | |
./go_js_wasm_exec test.wasm | |
echo | |
echo 'GopherJS' | |
gopherjs build -o test.js . | |
node test.js | |
echo | |
echo 'JavaScript' | |
node main.js | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment