Skip to content

Instantly share code, notes, and snippets.

@pbosetti
Created March 30, 2011 23:26
Show Gist options
  • Save pbosetti/895517 to your computer and use it in GitHub Desktop.
Save pbosetti/895517 to your computer and use it in GitHub Desktop.
Testing zlib compression on strings
#!/usr/bin/env ruby
# untitled.rb
# Created by Paolo Bosetti on 2011-03-30.
# Copyright (c) 2011 University of Trento. All rights reserved.
require "zlib"
str = ""
100.times do |i|
str << ("%d\t%9.3f\t%9.3f\n" % [i, i.to_f, i.to_f ** 2])
end
level = [Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, Zlib::DEFAULT_COMPRESSION] + (0..9).to_a
strz = ""
level.each do |l|
strz = Zlib::Deflate.deflate(str, l)
puts "level #{l}: #{str.size} -> #{strz.size}"
end
puts Zlib::Inflate.inflate(strz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment