Created
June 16, 2022 10:47
-
-
Save zacharysyoung/44bc98afe8799ac291c9969e9a918beb to your computer and use it in GitHub Desktop.
Go VS Python, silly but real metrics
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 | |
func main() { | |
for i := 0; i < N; i++ { | |
} | |
} |
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
def main(): | |
for _ in range(int(N)): | |
pass | |
if __name__ == "__main__": | |
main() |
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 | |
// Parse the timings-only file, big_loops.txt from run.sh, and print CSV. | |
import ( | |
"bufio" | |
"encoding/csv" | |
"log" | |
"os" | |
"strings" | |
) | |
func main() { | |
fname := os.Args[1] | |
f, err := os.Open(fname) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
records := [][]string{ | |
{"Magnitude_N", "Prog", "User_s", "System_s", "CPU_%", "Total_s"}, | |
} | |
r := bufio.NewScanner(f) | |
for r.Scan() { | |
line := r.Text() | |
var split []string | |
var rem string | |
var mag_n, prog, user, system, cpu, total string | |
// Start "popping" text off the front of the line... | |
split = strings.SplitN(line, " ", 2) | |
mag_n = split[0] | |
rem = split[1] | |
split = strings.SplitN(rem, " ", 2) // double space separates cmd and timings | |
prog = split[0] | |
rem = split[1] | |
split = strings.SplitN(rem, " user ", 2) | |
user = split[0] | |
rem = split[1] | |
split = strings.SplitN(rem, " system ", 2) | |
system = split[0] | |
rem = split[1] | |
split = strings.SplitN(rem, " cpu ", 2) | |
cpu = split[0] | |
rem = split[1] | |
split = strings.SplitN(rem, " total", 2) | |
total = split[0] | |
rem = split[1] | |
// Get rid of unwanted symbols, and some junk in the program name | |
mag_n = rmv(mag_n, "s") | |
user = rmv(user, "s") | |
system = rmv(system, "s") | |
cpu = rmv(cpu, "%") | |
total = rmv(total, "s") | |
prog = rmv(rmv(rmv(prog, " 2>&3"), "python3 "), "go run ") | |
records = append(records, []string{mag_n, prog, user, system, cpu, total}) | |
} | |
w := csv.NewWriter(os.Stdout) | |
w.WriteAll(records) | |
if err := w.Error(); err != nil { | |
log.Fatalln("error writing csv:", err) | |
} | |
} | |
func rmv(s string, old string) string { | |
return strings.Replace(s, old, "", -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
#!/bin/zsh | |
rm -f big_loops.txt big_loops_mem.txt | |
touch big_loops.txt big_loops_mem.txt | |
# https://unix.stackexchange.com/a/126057/366399 for built-in redirect (ZSH's time cmd) | |
for n in '1e6' '1e7' '1e8' '1e9'; do | |
for i in 1 2 3; do | |
echo $n $i | |
sed s/N/$n/g main.go > big_loops.go | |
echo -n "$n " >> big_loops.txt | |
{ time go run big_loops.go 2>&3; } 3>&2 2>> big_loops.txt | |
echo -n "$n big_loops.go " >> big_loops_mem.txt | |
/usr/bin/time -l go run big_loops.go 2>> big_loops_mem.txt | |
sed s/N/$n/g main.py > big_loops.py | |
echo -n "$n " >> big_loops.txt | |
{ time python3 big_loops.py 2>&3; } 3>&2 2>> big_loops.txt | |
echo -n "$n big_loops.py " >> big_loops_mem.txt | |
/usr/bin/time -l python3 big_loops.py 2>> big_loops_mem.txt | |
done | |
done | |
# Discard noisy lines, keep only timings and 'peak memory' | |
grep 'big_loops\|peak memory' big_loops_mem.txt > tmp | |
mv tmp big_loops_mem.txt | |
rm big_loops.go big_loops.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment