Created
May 30, 2017 18:58
-
-
Save index0h/aa46ce4c575920442e560112d95b7c48 to your computer and use it in GitHub Desktop.
go vs node
This file contains hidden or 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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"sort" | |
"time" | |
) | |
func bubbleSort(arr []int) []int { | |
length := len(arr) | |
swapped := false | |
for i := 0; i < length; i++ { | |
swapped = false | |
for j := 0; j < length-i-1; j++ { | |
if arr[j] > arr[j+1] { | |
arr[j], arr[j+1] = arr[j+1], arr[j] | |
swapped = true | |
} | |
} | |
if !swapped { | |
break | |
} | |
} | |
return arr | |
} | |
func main() { | |
var arr []int | |
var arr2 []int | |
{ | |
file, _ := ioutil.ReadFile("./data.json") | |
json.Unmarshal(file, &arr) | |
json.Unmarshal(file, &arr2) | |
} | |
start := time.Now() | |
bubbleSort(arr) | |
diff := time.Since(start).Nanoseconds() | |
fmt.Printf("Golang (bubble). count: %d; time (ns): %d\n", len(arr), diff) | |
start = time.Now() | |
sort.Ints(arr2) | |
diff = time.Since(start).Nanoseconds() | |
fmt.Printf("Golang (native). count: %d; time (ns): %d\n", len(arr2), diff) | |
} |
This file contains hidden or 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
var process = require('process'); | |
var bubble_sort = function(array){ | |
var length = array.length; | |
var swapped = false; | |
var tmp; | |
for (var i = 0; i < length; i++){ | |
swapped = false; | |
for ( var j = 0; j < length - i - 1; j++){ | |
if (array[j] > array[j + 1] ){ | |
tmp = array[j]; | |
array[j] = array[j+1]; | |
array[j+1] = tmp; | |
swapped = true; | |
} | |
} | |
if (!swapped){ | |
break; | |
} | |
} | |
return array; | |
}; | |
var sortFunc = (a, b) => a-b; | |
var arr = require('./data.json'); | |
var arr2 = arr.slice(); | |
var count = arr.length; | |
var start = process.hrtime(); | |
bubble_sort(arr); | |
var diff = process.hrtime(start); | |
console.log(`NodeJS (bubble). count: ${count}; time (ns): ${diff[0] * 1e9 + diff[1]}`); | |
start = process.hrtime(); | |
arr2.sort(sortFunc); | |
diff = process.hrtime(start); | |
console.log(`NodeJS (native). count: ${count}; time (ns): ${diff[0] * 1e9 + diff[1]}`); |
This file contains hidden or 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 ( | |
"encoding/json" | |
"io/ioutil" | |
"math/rand" | |
) | |
func generateArray(size int) []int { | |
var arr = make([]int, size) | |
index := 0 | |
for index < size { | |
arr[index] = rand.Intn(size) | |
index++ | |
} | |
return arr | |
} | |
func main() { | |
arr := generateArray(100000) | |
data, _ := json.Marshal(arr) | |
ioutil.WriteFile("./data.json", data, 0777) | |
} |
This file contains hidden or 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
Golang (bubble). count: 100000; time (ns): 15028623013 | |
Golang (native). count: 100000; time (ns): 16094116 | |
NodeJS (bubble). count: 100000; time (ns): 17965422412 | |
NodeJS (native). count: 100000; time (ns): 37253118 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment