Article View: news.software.nntp
Article #87236Re: About compression ratio
From: Julien ÉLIE
Date: Mon, 16 Nov 2015 19:12
Date: Mon, 16 Nov 2015 19:12
128 lines
4496 bytes
4496 bytes
Hi all,
For those interested in testing themselves, here is the script I used.
The "samples/strings" file contains 1-line commands or answers (one per line). The other files are multi-line answers.
% tree
..
|-- deflate_compare.py
`-- samples
|-- article-answer
|-- article-base64-attachment-answer
|-- capabilities-answer
|-- hdr-subject-answer-8604-articles
|-- help-answer
|-- list-active-answer-29462-newsgroups
|-- list-active.times-answer-13879-newsgroups
|-- list-counts-answer-29462-newsgroups
|-- listgroup-answer-with-8605-article-numbers
|-- list-moderators-answer
|-- list-newsgroups-answer-46026-newsgroups
|-- list-overview.fmt-answer
|-- newnews-answer-11096-mid
|-- over-answer-1-article
|-- over-answer-8604-articles
`-- strings
1 directory, 17 files
#!/usr/bin/python
import glob
import os, os.path
import zlib
SAMPLES_DIR = "samples"
STRINGS_FILE = "strings"
STRINGS_PATH = os.path.join(SAMPLES_DIR, STRINGS_FILE)
METHOD = zlib.DEFLATED
WINDOW_SIZE = -15
MEMLEVEL = 9
# Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY
STRATEGY = zlib.Z_DEFAULT_STRATEGY
first = True
if os.path.isfile(STRINGS_PATH):
for line in file(STRINGS_PATH):
if not first:
print
print
else:
first = False
# Ensure the command ends with CRLF.
line = line.rstrip("\r\n")
print line + "\\r\\n"
print '-'*(len(line)+4)
print
line += "\r\n"
print "Uncompressed: %d bytes" % len(line)
# Compress the command.
for level in xrange(0, 10):
encoder = zlib.compressobj(level, METHOD, WINDOW_SIZE, \
MEMLEVEL, STRATEGY)
zdata = encoder.compress(line)
zdata += encoder.flush(zlib.Z_FULL_FLUSH)
print "Compressed with level %d: %d bytes (%d%%)" % \
(level, len(zdata), 100*len(zdata)/len(line))
else:
print "No file found with name %s" % STRINGS_FILE
first = False
for filename in sorted(os.listdir(SAMPLES_DIR)):
if filename == STRINGS_FILE:
continue
if not first:
print
print
else:
first = False
label = "Processing %s" % filename
print label
print '-'*len(label)
print
# Read the file and ensure every line ends with CRLF.
f = open(os.path.join(SAMPLES_DIR, filename), 'r')
lines = f.readlines()
data = []
flen = 0
for line in lines:
line = line.rstrip("\r\n") + "\r\n"
flen += len(line)
data.append(line)
f.close()
print "Uncompressed: %d bytes" % flen
# Compress the file.
for level in xrange(0, 10):
encoder = zlib.compressobj(level, METHOD, WINDOW_SIZE, \
MEMLEVEL, STRATEGY)
zdata = ''
for line in data:
zdata += encoder.compress(line)
zdata += encoder.flush(zlib.Z_FULL_FLUSH)
print "Compressed with level %d: %d bytes (%d%%)" % \
(level, len(zdata), 100*len(zdata)/flen)
--
Julien ÉLIE
« C'est comme chercher une aiguille dans du foin en bottes ! »
(Jolitorax)
Technical Details
Message-ID:
<n2d9q8$4ed$1@news.trigofacile.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.(19219).mbox.7z!not-for-mailReferences:
<n2d9ha$41r$1@news.trigofacile.com>Show Full Headers
Complete Article Headers
Message-ID: <n2d9q8$4ed$1@news.trigofacile.com> Subject: Re: About compression ratio Date: Mon, 16 Nov 2015 20:12:08 +0100 From: Julien ÉLIE <iulius@nom-de-mon-site.com.invalid> References: <n2d9ha$41r$1@news.trigofacile.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.(19219).mbox.7z!not-for-mail Newsgroups: news.software.nntp Organization: TrigoFACILE -- http://www.trigofacile.com/ Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 16 Nov 2015 19:12:08 -0000 (UTC) Injection-Info: news.trigofacile.com; posting-account="julien"; posting-host="aaubervilliers-651-1-341-170.w83-114.abo.wanadoo.fr:83.114.228.170"; logging-data="4557"; mail-complaints-to="abuse@trigofacile.com" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <n2d9ha$41r$1@news.trigofacile.com> X-Received-Body-CRC: 3952161564 X-Received-Bytes: 4479