Last active
February 22, 2024 08:59
-
-
Save janklimo/240cf6b4517941f52809343631daff77 to your computer and use it in GitHub Desktop.
Comparison of memory usage: AXLSX vs. rubyXL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'axlsx' | |
require 'rubyXL' | |
require 'rubyXL/convenience_methods/worksheet' | |
require 'memory_profiler' | |
rows = 1_000 | |
columns = 20 | |
report = MemoryProfiler.report do | |
# axlsx | |
Axlsx::Package.new do |p| | |
p.workbook.add_worksheet(name: 'Test') do |sheet| | |
rows.times do | |
sheet.add_row ['test data'] * columns | |
end | |
end | |
p.serialize('tmp/test_axlsx.xlsx') | |
end | |
# rubyXL | |
workbook = RubyXL::Workbook.new | |
worksheet = workbook.worksheets[0] | |
worksheet.sheet_name = 'Test' | |
rows.times do |r| | |
worksheet.insert_row(r) | |
columns.times do |c| | |
worksheet.insert_cell(r, c, 'test data') | |
end | |
end | |
workbook.write('tmp/test_rubyxl.xlsx') | |
end | |
report.pretty_print | |
# rows = 100 | |
# columns = 20 | |
# | |
# retained memory by gem | |
# ----------------------------------- | |
# 119888 axlsx-3.0.0.pre | |
# 5384 rubyXL-3.3.30 | |
# 1527 pathname | |
# 195 rubyzip-1.2.1 | |
# 80 htmlentities-4.3.4 | |
# 40 nokogiri-1.8.4 | |
# 40 other | |
# 40 singleton | |
# | |
# =================================== | |
# | |
# rows = 1_000 | |
# columns = 20 | |
# | |
# retained memory by gem | |
# ----------------------------------- | |
# 1102664 axlsx-3.0.0.pre | |
# 5384 rubyXL-3.3.30 | |
# 1527 pathname | |
# 195 rubyzip-1.2.1 | |
# 80 htmlentities-4.3.4 | |
# 40 nokogiri-1.8.4 | |
# 40 other | |
# 40 singleton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rerun with newer versions and using
caxlsx
.I tried with 100,000 rows but it reached 32GB of RAM used before I stopped the process.
So the memory retained by
rubyXL
is constant but there is a tradeoff:100 rows
1,000 rows
rubyXL
allocates significantly more memory thancaxlsx
. While it doesn't retain it, it will require more RAM to run :/ Time for a new approach.fast_excel
gem1,000 rows
1,000,000 rows
Finished in 26s without missing a beat ⚡ There was no retained memory and ruby's process peaked at 800MB on my machine. Compare that to 32GB of memory used by the other two gems before I terminated the benchmark. Promising 🤞