Skip to content

Instantly share code, notes, and snippets.

@alrra
Last active April 12, 2016 08:09
Show Gist options
  • Save alrra/5484214 to your computer and use it in GitHub Desktop.
Save alrra/5484214 to your computer and use it in GitHub Desktop.
Simple shell script to demonstrate the difference in compression ratio and compression speed between Gzip and Zopfli with different settings.
#!/bin/bash
# Zopfli information and source:
# https://code.google.com/p/zopfli/
# Usage:
# ./zopfli.sh [URL] [file] ...
declare dateCmd=""
declare -a zopfliIterationValues=( "1" "5" "10" "20" "50" "100" "500" "1000" )
diff() {
printf "%s %s" "$1 $2" | \
awk '{
x = $1 - $2;
if ( x > 0 ) { printf "+"; }
printf "%d", x;
}'
}
get_time() {
printf "%s" "$($dateCmd)"
}
percentage() {
printf "%s %s" "$1 $2" | \
awk '{
x = $2 * 100 / $1 - 100;
if ( x > 0 ) { printf "+"; }
printf "%.2f%%", x;
}'
}
print_header() {
printf "\n"
print_line
print_row "Method" "Size diff (b)" " Size ↑/↓" "Time diff (ns)" " Time ↑/↓"
print_line
}
print_line() {
printf "%s\n" \
" ----------------------------------------------------------------------------- "
}
print_line_dotted() {
printf "%s\n" \
" | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | "
}
print_row() {
printf " | %-14s | %13s | %9s | %15s | %10s |\n" "$1" "$2" "$3" "$4" "$5"
}
print_stats() {
declare -i os=0 # original size
declare -i gs=0 # gzip size
declare -i gt=0 # gzip time
declare -i dzs=0 # default zopfli size
declare -i dzt=0 # declare zopfli time
declare -i zs=0 # zopfli size
declare -i zt=0 # zopfli time
declare -i st=0 # start time
declare -i et=0 # end time
if [ -f "$1" ]; then
if [ -s "$1" ]; then
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print_header
os=$(
stat -f%z "$1" 2> /dev/null; # OS X `stat`
stat -c%s "$1" 2> /dev/null; # GNU `stat`
)
print_row "original" "$os"
print_line_dotted
st=$( get_time )
gs=$( gzip -c "$1" | wc -c )
et=$( get_time )
gt=$(( $et - $st ))
print_row "gzip" "$(diff $gs $os)" "$(percentage $os $gs)" "$gt"
st=$( get_time )
dzs=$( zopfli -c "$1" | wc -c )
et=$( get_time )
dzt=$(( $et - $st ))
print_row "zopfli | 15" "$(diff $dzs $os)" "$(percentage $os $dzs)" "$dzt"
print_line
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print_header
print_row "zopfli | 15" "$dzs" "" "$dzt"
print_line_dotted
print_row \
"gzip" \
"$( diff $gs $dzs )" \
"$( percentage $dzs $gs)" \
"$( diff $gt $dzt )" \
"$( percentage $dzt $gt)"
for i in ${zopfliIterationValues[@]}; do
st=$( get_time )
zs=$( zopfli -c --i"$i" "$1" | wc -c )
et=$( get_time )
zt=$(( $et - $st ))
print_row \
"zopfli | $(printf "%-5s" "$i")" \
"$( diff $zs $dzs )" \
"$( percentage $dzs $zs)" \
"$( diff $zt $dzt )" \
"$( percentage $dzt $zt)"
done
print_line
printf "\n"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
else
printf "%s" "'$1' is empty."
fi
else
printf "%s" "'$1' is not a file."
fi
}
main() {
local tmpFile=""
# The GNU `date` command has the `%N` interpreted sequence while other
# implementations don't. On MAC OS X `gdate` can be used instead of the
# native `date` if the `coreutils` package was installed.
if [ "$(date +%N)" != "N" ] || [ ! -x "$(command -v 'gdate')" ]; then
dateCmd="date +%s%N"
else
dateCmd="gdate +%s%N"
fi
while [ $# -gt 0 ]; do
printf "\n\n %s:\n" "$1"
# local file
if [ -f "$1" ]; then
print_stats "$1"
# URL
else
# Create local (temporary copy) of the URL's content
tmpFile=$(mktemp "/tmp/XXXXX");
curl -A "Mozilla/5.0 Gecko" --connect-timeout 30 -m 60 \
-LsS "$1" > "$tmpFile"
print_stats "$tmpFile"
rm -rf "$tmpFile"
fi
shift 1
done
}
main "$@"
./zopfli_test.sh \
https://raw.github.com/necolas/normalize.css/v1/normalize.css \
http://modernizr.com/i/css/modernizr-2.1.1.css \
http://code.jquery.com/jquery-1.9.1.min.js \
http://code.jquery.com/jquery-1.9.1.js \
http://newevolutiondesigns.com/images/freebies/hd-wallpaper-6.jpg \
http://freedownloads.last.fm/download/204685484/The%2BMost%2BBeautiful%2BDay.mp3 \
https://github.com/h5bp/html5boilerplate.com/commit/ce994ab88d2551de9a4a87d53bfc0c384e0dfcd5
https://raw.github.com/necolas/normalize.css/v1/normalize.css:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 9802 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -7086 | -72.29% | 7053000 | |
| zopfli | 15 | -7160 | -73.05% | 50088000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 2642 | | 50088000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +74 | +2.80% | -43035000 | -85.92% |
| zopfli | 1 | +8 | +0.30% | -32451000 | -64.79% |
| zopfli | 5 | +2 | +0.08% | -25484000 | -50.88% |
| zopfli | 10 | 0 | 0.00% | -11467000 | -22.89% |
| zopfli | 20 | 0 | 0.00% | +19532000 | +39.00% |
| zopfli | 50 | 0 | 0.00% | +101989000 | +203.62% |
| zopfli | 100 | -1 | -0.04% | +188511000 | +376.36% |
| zopfli | 500 | -2 | -0.08% | +1156860000 | +2309.66% |
| zopfli | 1000 | -2 | -0.08% | +2166677000 | +4325.74% |
-----------------------------------------------------------------------------
http://modernizr.com/i/css/modernizr-2.1.1.css:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 43134 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -31730 | -73.56% | 9526000 | |
| zopfli | 15 | -32271 | -74.82% | 215431000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 10863 | | 215431000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +541 | +4.98% | -205905000 | -95.58% |
| zopfli | 1 | +48 | +0.44% | -141632000 | -65.74% |
| zopfli | 5 | +4 | +0.04% | -93535000 | -43.42% |
| zopfli | 10 | +4 | +0.04% | -44302000 | -20.56% |
| zopfli | 20 | -2 | -0.02% | +68875000 | +31.97% |
| zopfli | 50 | -2 | -0.02% | +384183000 | +178.33% |
| zopfli | 100 | -4 | -0.04% | +882546000 | +409.67% |
| zopfli | 500 | -9 | -0.08% | +4758475000 | +2208.82% |
| zopfli | 1000 | -10 | -0.09% | +9105194000 | +4226.50% |
-----------------------------------------------------------------------------
http://code.jquery.com/jquery-1.9.1.min.js:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 92629 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -59929 | -64.70% | 11946000 | |
| zopfli | 15 | -60938 | -65.79% | 369602000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 31691 | | 369602000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +1009 | +3.18% | -357656000 | -96.77% |
| zopfli | 1 | +26 | +0.08% | -244817000 | -66.24% |
| zopfli | 5 | 0 | 0.00% | -167074000 | -45.20% |
| zopfli | 10 | 0 | 0.00% | -82888000 | -22.43% |
| zopfli | 20 | 0 | 0.00% | +47415000 | +12.83% |
| zopfli | 50 | 0 | 0.00% | +572002000 | +154.76% |
| zopfli | 100 | -2 | -0.01% | +1327818000 | +359.26% |
| zopfli | 500 | -2 | -0.01% | +7405354000 | +2003.60% |
| zopfli | 1000 | -5 | -0.02% | +15003741000 | +4059.43% |
-----------------------------------------------------------------------------
http://code.jquery.com/jquery-1.9.1.js:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 268381 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -188658 | -70.29% | 27699000 | |
| zopfli | 15 | -192651 | -71.78% | 1200680000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 75730 | | 1200680000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +3993 | +5.27% | -1172981000 | -97.69% |
| zopfli | 1 | +342 | +0.45% | -779146000 | -64.89% |
| zopfli | 5 | +81 | +0.11% | -532469000 | -44.35% |
| zopfli | 10 | +14 | +0.02% | -340525000 | -28.36% |
| zopfli | 20 | -1 | -0.00% | +155331000 | +12.94% |
| zopfli | 50 | -3 | -0.00% | +1754346000 | +146.11% |
| zopfli | 100 | -44 | -0.06% | +3913245000 | +325.92% |
| zopfli | 500 | -70 | -0.09% | +23549846000 | +1961.38% |
| zopfli | 1000 | -74 | -0.10% | +45396528000 | +3780.90% |
-----------------------------------------------------------------------------
http://newevolutiondesigns.com/images/freebies/hd-wallpaper-6.jpg:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 2025972 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -26042 | -1.29% | 89393000 | |
| zopfli | 15 | -28069 | -1.39% | 5483800000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 1997903 | | 5483800000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +2027 | +0.10% | -5394407000 | -98.37% |
| zopfli | 1 | +10 | +0.00% | -3457457000 | -63.05% |
| zopfli | 5 | +1 | +0.00% | -2420878000 | -44.15% |
| zopfli | 10 | 0 | 0.00% | -1175738000 | -21.44% |
| zopfli | 20 | -1 | -0.00% | +1457458000 | +26.58% |
| zopfli | 50 | -43 | -0.00% | +8792915000 | +160.34% |
| zopfli | 100 | -43 | -0.00% | +22463403000 | +409.63% |
| zopfli | 500 | -52 | -0.00% | +114677288000 | +2091.20% |
| zopfli | 1000 | -52 | -0.00% | +233675108000 | +4261.19% |
-----------------------------------------------------------------------------
http://freedownloads.last.fm/download/204685484/The%2BMost%2BBeautiful%2BDay.mp3:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 6254953 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -40300 | -0.64% | 309280000 | |
| zopfli | 15 | -52285 | -0.84% | 18354451000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 6202668 | | 18354451000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +11985 | +0.19% | -18045171000 | -98.31% |
| zopfli | 1 | +717 | +0.01% | -9366048000 | -51.03% |
| zopfli | 5 | +6 | +0.00% | -6654424000 | -36.26% |
| zopfli | 10 | +4 | +0.00% | -3357270000 | -18.29% |
| zopfli | 20 | 0 | 0.00% | +3448865000 | +18.79% |
| zopfli | 50 | -5 | -0.00% | +27241159000 | +148.42% |
| zopfli | 100 | -7 | -0.00% | +59637902000 | +324.92% |
| zopfli | 500 | -24 | -0.00% | +334810449000 | +1824.14% |
| zopfli | 1000 | -45 | -0.00% | +672684154000 | +3664.96% |
-----------------------------------------------------------------------------
https://github.com/h5bp/html5boilerplate.com/commit/ce994ab88d2551de9a4a87d53bfc0c384e0dfcd5:
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| original | 28132050 | | | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | -26854997 | -95.46% | 274218000 | |
| zopfli | 15 | -26995095 | -95.96% | 272359415000 | |
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
| Method | Size diff (b) | Size ↑/↓ | Time diff (ns) | Time ↑/↓ |
-----------------------------------------------------------------------------
| zopfli | 15 | 1136955 | | 272359415000 | |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| gzip | +140098 | +12.32% | -272085197000 | -99.90% |
| zopfli | 1 | +7025 | +0.62% | -215257373000 | -79.03% |
| zopfli | 5 | +322 | +0.03% | -157270918000 | -57.74% |
| zopfli | 10 | +101 | +0.01% | -82746284000 | -30.38% |
| zopfli | 20 | -196 | -0.02% | +73501590000 | +26.99% |
| zopfli | 50 | -1106 | -0.10% | +532736471000 | +195.60% |
| zopfli | 100 | -1204 | -0.11% | +1385962785000 | +508.87% |
| zopfli | 500 | -1432 | -0.13% | +7759818754000 | +2849.11% |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| zopfli | 1000 | it took too long and I didn't care anymore :P |
-----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment