Skip to content

Commit 3e5a45e

Browse files
committed
Merge remote-tracking branch 'origin/text_auto_width' into auto_width
2 parents d5bf25f + 1b3132c commit 3e5a45e

14 files changed

+914
-10
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.ruby-version
2+
.idea/
23
try.c
34
try.rb
45
gen.rb
@@ -11,6 +12,6 @@ libxlsxwriter/dev
1112
libxlsxwriter/docs
1213
libxlsxwriter/examples
1314
libxlsxwriter/test
14-
15+
ext/fast_excel/text_width_ext.o
1516
ext/fast_excel/Makefile
1617
ext/fast_excel/text_width_ext.bundle

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Q=
44
endif
55

66
all :
7+
@echo "Compiling ext/text_width ..."
8+
rake compile
79
@echo "Compiling libxlsxwriter ..."
810
$(Q)$(MAKE) -C libxlsxwriter
911

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ task :examples do
2222
require './' + file.sub(/\.rb$/, '')
2323
end
2424
end
25+
26+
task :compile do
27+
%x{
28+
cd ext/fast_excel
29+
ruby ./extconf.rb
30+
make
31+
}
32+
end

benchmarks/auto_width.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative 'init'
2+
3+
HEADERS = ["id", "name", "age", "date"]
4+
5+
DATA = []
6+
1000.times do |n|
7+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
8+
end
9+
10+
Benchmark.ips do |x|
11+
x.config(time: 10, warmup: 2)
12+
13+
x.report("Normal") do
14+
workbook = FastExcel.open(constant_memory: true)
15+
worksheet = workbook.add_worksheet("benchmark")
16+
17+
worksheet.write_row(0, HEADERS)
18+
DATA.each_with_index do |row, i|
19+
worksheet.write_row(i + 1, row)
20+
end
21+
workbook.read_string
22+
end
23+
24+
x.report("With auto_width") do
25+
workbook = FastExcel.open(constant_memory: true)
26+
worksheet = workbook.add_worksheet("benchmark")
27+
worksheet.auto_width = true
28+
29+
worksheet.write_row(0, HEADERS)
30+
DATA.each_with_index do |row, i|
31+
worksheet.write_row(i + 1, row)
32+
end
33+
workbook.read_string
34+
end
35+
36+
x.compare!
37+
end

benchmarks/profiler.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative '../lib/fast_excel'
2+
require 'ruby-prof'
3+
4+
DATA = []
5+
1000.times do |n|
6+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
7+
end
8+
9+
RubyProf.start
10+
11+
100.times do
12+
workbook = FastExcel.open(constant_memory: true)
13+
worksheet = workbook.add_worksheet("benchmark")
14+
worksheet.auto_width = true
15+
16+
DATA.each_with_index do |row, i|
17+
worksheet.write_row(i + 1, row)
18+
end
19+
workbook.read_string
20+
21+
print '.'
22+
end
23+
result = RubyProf.stop
24+
25+
# print a flat profile to text
26+
printer = RubyProf::FlatPrinter.new(result)
27+
printer.print(STDOUT)

benchmarks/write_value.rb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require_relative '../lib/fast_excel'
2+
require "benchmark/memory"
3+
require "benchmark/ips"
4+
5+
DATA = []
6+
1000.times do |n|
7+
DATA << [n, "String string #{n}", (n * rand * 10).round, Time.at(n * 1000 + 1492922688)]
8+
end
9+
10+
5.times do
11+
workbook = FastExcel.open(constant_memory: true)
12+
worksheet = workbook.add_worksheet("benchmark")
13+
14+
DATA.each_with_index do |row, row_num|
15+
row.each_with_index do |val, cell_num|
16+
worksheet.write_value(row_num + 1, cell_num + 1, val)
17+
end
18+
end
19+
workbook.read_string
20+
end
21+
22+
23+
workbook = FastExcel.open(constant_memory: true)
24+
worksheet = workbook.add_worksheet("benchmark")
25+
26+
DATA.each_with_index do |row, row_num|
27+
row.each_with_index do |val, cell_num|
28+
worksheet.write_value(row_num + 1, cell_num + 1, val)
29+
end
30+
end
31+
workbook.read_string
32+
33+
Benchmark.ips do |x|
34+
#x.config(time: 10, warmup: 2)
35+
36+
x.report("Normal") do
37+
workbook = FastExcel.open(constant_memory: true)
38+
worksheet = workbook.add_worksheet("benchmark")
39+
40+
DATA.each_with_index do |row, row_num|
41+
row.each_with_index do |val, cell_num|
42+
worksheet.write_value(row_num + 1, cell_num + 1, val)
43+
end
44+
end
45+
workbook.read_string
46+
end
47+
48+
x.report("Auto-width") do
49+
workbook = FastExcel.open(constant_memory: true)
50+
worksheet = workbook.add_worksheet("benchmark")
51+
worksheet.auto_width = true
52+
53+
DATA.each_with_index do |row, row_num|
54+
row.each_with_index do |val, cell_num|
55+
worksheet.write_value(row_num + 1, cell_num + 1, val)
56+
end
57+
end
58+
workbook.read_string
59+
end
60+
61+
x.compare!
62+
end

examples/example_auto_width.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative '../lib/fast_excel'
2+
3+
workbook = FastExcel.open("example_auto_width.xlsx", constant_memory: false)
4+
5+
# this is required to make auto-width works correctly
6+
workbook.default_format.set(
7+
font_size: 13,
8+
font_family: "Arial"
9+
)
10+
11+
worksheet = workbook.add_worksheet
12+
worksheet.auto_width = true
13+
14+
['Arial', 'Calibri', 'Times New Roman'].each_with_index do |font, index|
15+
col_format = workbook.add_format(font_family: font, font_size: 17)
16+
worksheet.set_column(index * 4, index * 4 + 3, 10, col_format)
17+
18+
worksheet.write_value(0, index * 4 + 2, font)
19+
worksheet.write_value(1, index * 4, "tini")
20+
worksheet.write_value(1, index * 4 + 1, "Longer")
21+
worksheet.write_value(1, index * 4 + 2, "Some longer text!")
22+
worksheet.write_value(1, index * 4 + 3, "This gem is FFI binding for libxlsxwriter C library")
23+
end
24+
25+
workbook.close
26+
puts "Saved to file example_auto_width.xlsx"

ext/fast_excel/extconf.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'mkmf'
2+
3+
create_makefile('ext/text_width_ext')

0 commit comments

Comments
 (0)