Skip to content

Instantly share code, notes, and snippets.

@jul
Last active Jan 24, 2022
Embed
What would you like to do?
timing python oneliner by size of line
#!/usr/bin/env bash
export LANG=C
redadd() { python3 -c '(s,d,r)=(__import__("sys"),__import__("archery").mdict,__import__("functools").reduce);print(sorted(r(d.__add__,(d({l[:-1]:1}) for l in s.stdin.readlines())).items(),key=lambda k:k[1],reverse=True)[:10])' ; }
red() { python3 -c '(s,d,r)=(__import__("sys"),__import__("archery").mdict,__import__("functools").reduce);print(sorted(r(d.__radd__,(d({l[:-1]:1}) for l in s.stdin.readlines())).items(),key=lambda k:k[1],reverse=True)[:10])' ; }
function count() { python3 -c'(s,d)=(__import__("sys"),__import__("archery").mdict);print(sorted(sum((d({l[:-1]:1}) for l in s.stdin.readlines()),{}).items(),key=lambda k:k[1],reverse=True)[:10])'; }
cfl() { python3 -c'(s,t,d,f)=(__import__("sys"),__import__("tld"),__import__("archery").mdict,lambda l:t.get_fld(l[:-1],fix_protocol=True,fail_silently=True));print(sorted(sum((d({f(l):1}) for l in s.stdin.readlines()),{}).items(),key=lambda k:k[1],reverse=True)[:10])'; }
test_time() {
func="$1"
TIMEFORMAT="%U"
printf " %.5f" $( echo "$( (time $func < this.sample 1> /dev/null) 2>&1 ) / $l" | bc -l );
}
OUT="out.$(date -Iminutes).png"
(echo line_count sum reduce redadd;
for((l=100; l<=3000; l+=500)); do
echo -n "$l"; head -n $l this.list > this.sample ;
test_time count;test_time red;test_time redadd; echo;
done ) | (
cat > /dev/shm/mytempfile && \
trap 'rm /dev/shm/mytempfile' EXIT && \
#gnuplot -e "set terminal dumb; plot for[col=2:4] '/dev/shm/mytempfile' using 1:col title columnheader(col) with lines")
gnuplot -e "set terminal png; set output '${OUT}'; plot for[col=2:4] '/dev/shm/mytempfile' using 1:col title columnheader(col) with lines") && qiv $OUT
#redadd < this.list
@jul
Copy link
Author

jul commented Jan 24, 2022

Je crois que c'est pratique de pouvoir faire des oneliners rapides :)

out 2022-01-25T00:34+01:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment