Last active
December 2, 2020 01:08
-
-
Save zhongxiao37/0accf1e8df1a2063689f515a76372ff1 to your computer and use it in GitHub Desktop.
Count the frequency of 10M integers
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 "fmt" | |
import "math/rand" | |
import "time" | |
func main(){ | |
const n = 10000000 | |
var arr [n]int | |
var counter [100]int | |
for i := 0; i < n; i++ { | |
arr[i] = rand.Intn(100) | |
} | |
for i := 0; i < 100; i++ { | |
counter[i] = 0 | |
} | |
t1 := time.Now() | |
for i := 0; i < n; i++ { | |
counter[arr[i]] += 1 | |
} | |
elapsed := time.Since(t1) | |
fmt.Println("elapsed: ", elapsed) | |
fmt.Println(counter) | |
} |
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
import numpy as np | |
import time | |
import sys | |
import random | |
def time_it(func): | |
def clock(*args): | |
t0 = time.time() | |
result = func(*args) | |
elapsed = time.time() - t0 | |
output = '%s Total time: %0.8f sec' % (func.__name__, elapsed) | |
print(output) | |
return result | |
return clock | |
@time_it | |
def np_int_counter(arr): | |
data = {} | |
for i in range(0, 100): | |
data[i] = np.where(arr == i, 1, 0).sum() | |
return data | |
@time_it | |
def counter_sort_array(arr): | |
counter = [0] * 100 | |
for i in arr: | |
counter[i] += 1 | |
return counter | |
@time_it | |
def counter_sort_array_v2(arr): | |
counter = [0] * 100 | |
try: | |
while True: | |
counter[arr.pop()] += 1 | |
except IndexError: | |
pass | |
return counter | |
@time_it | |
def counter_sort_dict(arr): | |
counter = {i: 0 for i in range(0, 100)} | |
for i in arr: | |
counter[i] += 1 | |
return counter | |
arr = [random.randrange(0, 100) for _ in range(10000000)] | |
print(sys.getsizeof(arr)) | |
counter_sort_array(arr) | |
counter_sort_dict(arr) | |
counter_sort_array_v2(arr) | |
arr = (random.randrange(0, 100) for _ in range(10000000)) | |
print(sys.getsizeof(arr)) | |
counter_sort_array(arr) | |
arr = (random.randrange(0, 100) for _ in range(10000000)) | |
counter_sort_dict(arr) | |
arr = np.random.randint(0, 100, size=10000000) | |
counter_sort_array(arr) | |
counter_sort_dict(arr) | |
np_int_counter(arr) |
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
require "benchmark" | |
arr = (0..10000000).map { rand(100) } | |
array_count = Benchmark.measure do | |
count = [0] * 100 | |
arr.each { |i| count[i] += 1 } | |
count | |
end | |
hash_count = Benchmark.measure do | |
count = (0..100).map { |i| [i, 0] }.to_h | |
arr.each { |i| count[i] += 1 } | |
count | |
end | |
puts array_count | |
puts hash_count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OutPut of Python
Output of Ruby
Output of Go