Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cema-sp committed Jun 24, 2016
0 parents commit ee5e138
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore OSX dir files
.DS_Store

# Ignore Vim swap files
*.swp

27 changes: 27 additions & 0 deletions 001_gc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/ruby

require 'benchmark'

no_gc = ARGV[0] == '-o'

num_rows = 100_000
num_cols = 10

data = Array.new(num_rows) do
Array.new(num_cols) { 'x' * 1000 }
end

mem_before = "%d MB" % (`ps -o rss= -p #{Process.pid}`.to_i / 1024)

GC.disable if no_gc
time = Benchmark.realtime do
csv = data.map do |row|
row.join(',')
end.join("\n")
end

mem_after = "%d MB" % (`ps -o rss= -p #{Process.pid}`.to_i / 1024)

puts "Time: #{time} (GC #{no_gc ? 'disabled' : 'enabled'})"
puts "MEM: #{mem_before} -> #{mem_after}"

30 changes: 30 additions & 0 deletions 002_memory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/ruby

require 'benchmark'

num_rows = 100_000
num_cols = 10

data = Array.new(num_rows) do
Array.new(num_cols) { 'x' * 1000 }
end

mem_before = "%d MB" % (`ps -o rss= -p #{Process.pid}`.to_i / 1024)

time = Benchmark.realtime do
csv = ''
num_rows.times do |i|
num_cols.times do |j|
csv << data[i][j]
csv << ',' unless j == num_cols - 1
end

csv << "\n" unless i == num_rows - 1
end
end

mem_after = "%d MB" % (`ps -o rss= -p #{Process.pid}`.to_i / 1024)

puts "Time: #{time}"
puts "MEM: #{mem_before} -> #{mem_after}"

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ruby Performance Optimization Book Examples

## Try to disable GC and define memory consumption

1. GC often makes Ruby slow (especialy <= v2.0 ). And that's because of high memory consumption
2. Ruby has significant memory overhead
3. GC in v2.1+ is 5 times faster!
4. Raw performance of v1.9 - v2.3 is about the same

See [001_gc.rb].

## Try to optimize memory

1. 80% of performance optimization comes from memory optimization

See [002_memory.rb]

0 comments on commit ee5e138

Please sign in to comment.