Skip to content

Instantly share code, notes, and snippets.

@keymastervn
Forked from nvh0412/benchmark.rb
Last active April 13, 2021 07:58
Show Gist options
  • Save keymastervn/f0293583708f15485d0da337ab8c2819 to your computer and use it in GitHub Desktop.
Save keymastervn/f0293583708f15485d0da337ab8c2819 to your computer and use it in GitHub Desktop.
Benchmark mini_mime vs marcel
require 'memory_profiler'
require 'benchmark/ips'
puts "Memory stats for requiring mini_mime"
result = MemoryProfiler.report do
require 'mini_mime'
end
puts "Total allocated: #{result.total_allocated_memsize} bytes (#{result.total_allocated} objects)"
puts "Memory stats for requiring marcel"
result = MemoryProfiler.report do
require 'marcel'
end
puts "Total allocated: #{result.total_allocated_memsize} bytes (#{result.total_allocated} objects)"
Benchmark.ips do |bm|
bm.report 'cached content_type lookup MiniMime' do
MiniMime.lookup_by_filename("a.txt").content_type
end
bm.report 'content_type lookup Marcel::MimeType' do
Marcel::MimeType.for "a.txt"
end
bm.compare!
end
module MiniMime
class Db
class RandomAccessDb
alias_method :lookup, :lookup_uncached
end
end
end
Benchmark.ips do |bm|
bm.report 'uncached content_type lookup MiniMime' do
MiniMime.lookup_by_filename("a.txt").content_type
end
bm.report 'content_type lookup Marcel::MimeType' do
Marcel::MimeType.for "a.txt"
end
bm.compare!
end
# ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]
# Memory stats for requiring mini_mime
# Total allocated: 478455 bytes (5677 objects)
# Memory stats for requiring marcel
# Total allocated: 1548021 bytes (23836 objects)
# Warming up --------------------------------------
# cached content_type lookup MiniMime
# 125.096k i/100ms
# content_type lookup Marcel::MimeType
# 434.000 i/100ms
# Calculating -------------------------------------
# cached content_type lookup MiniMime
# 1.243M (± 6.4%) i/s - 6.255M in 5.061474s
# content_type lookup Marcel::MimeType
# 4.416k (± 2.0%) i/s - 22.134k in 5.014166s
# Comparison:
# cached content_type lookup MiniMime: 1242769.7 i/s
# content_type lookup Marcel::MimeType: 4416.1 i/s - 281.42x (± 0.00) slower
# Warming up --------------------------------------
# uncached content_type lookup MiniMime
# 1.284k i/100ms
# content_type lookup Marcel::MimeType
# 398.000 i/100ms
# Calculating -------------------------------------
# uncached content_type lookup MiniMime
# 12.085k (± 7.1%) i/s - 60.348k in 5.024001s
# content_type lookup Marcel::MimeType
# 4.034k (± 5.6%) i/s - 20.298k in 5.048360s
# Comparison:
# uncached content_type lookup MiniMime: 12084.8 i/s
# content_type lookup Marcel::MimeType: 4034.4 i/s - 3.00x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment