|
1 | 1 | ---
|
2 | 2 | layout: post
|
3 | 3 | title: "Surprises in GopherJS Performance"
|
4 |
| -date: 2015-09-27 |
| 4 | +date: 2015-09-28 |
5 | 5 | author: Dmitri Shuralyov
|
6 | 6 | ---
|
7 | 7 |
|
@@ -100,14 +100,14 @@ So I looked at how [Go implements it](http://gotools.org/math#Pow).
|
100 | 100 | Pretty straightforward Go code.
|
101 | 101 | Now I knew GopherJS uses some JavaScript native APIs to implement parts of the standard library, so I checked how [it implemented `math.Pow`](https://github.com/gopherjs/gopherjs/blob/master/compiler/natives/math/math.go#L157).
|
102 | 102 | Aha! It's not the same code after all.
|
103 |
| -GopherJS implements it by using [JavaScript's `Math` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math), so it translates to: |
| 103 | +GopherJS implements it by using [JavaScript's `Math` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math), so it translates to the following JavaScript code: |
104 | 104 |
|
105 | 105 | ```JavaScript
|
106 | 106 | Math.pow(x, y)
|
107 | 107 | ```
|
108 | 108 |
|
109 | 109 | That's when it hit me.
|
110 |
| -In this code, which was taken from a snippet that optimized for brevity and demonstration purposes rather than performance, `math.Pow` was being used with th first argument of -1, and the second argument are values 0, 1, 2, 3, etc., in sequence. |
| 110 | +In this code, which was taken from a snippet that optimized for brevity and demonstration purposes rather than performance, `math.Pow` was being used with the first argument of -1, and the second argument are values 0, 1, 2, 3, etc., in sequence. |
111 | 111 | The output of that is an alternating sequence of 1, -1, 1, -1, 1, -1, etc.
|
112 | 112 | But using `math.Pow` for that is extremely inefficient, since it's meant to work with arbitrary inputs that are much harder to calculate.
|
113 | 113 | This can be trivially rewritten with an if statement.
|
@@ -190,7 +190,7 @@ total time taken is: 6.549s
|
190 | 190 |
|
191 | 191 | As expected, the GopherJS time did not change because it was a no-op, but the native Go performance has now caught up to the GopherJS version!
|
192 | 192 |
|
193 |
| -Just to be sure, I wanted to see if 6.5 seconds was as fast as these 1 billion iterations could happen, even if you were to implement this in a low level language like C: |
| 193 | +Just to be sure, I wanted to see if 6.5 seconds was as fast as these 1 billion iterations could happen, even if you were to implement this in a low-level language like C: |
194 | 194 |
|
195 | 195 | ```C
|
196 | 196 | #include <stdio.h>
|
|
0 commit comments