About performance, you might find this is interesting:
def trace(&block) printf "%8s | %-10s | %-20s | %-15s\n", 'event', 'line', 'id', 'classname' printf "%8s-+-%-10s-+-%-20s-+-%-15s\n", '-'*8, '-'*10, '-'*20, '-'*15 set_trace_func proc { |event, file, line, id, _, classname| printf "%8s | %-10d | %-20s | %-15s\n", event, line, id, classname unless ['trace','set_trace_func'].include? id.to_s } yield set_trace_func(nil) end puts "Method 1" trace do [1,2,3,4,5].sum end puts "\n\n" puts "Method 2" trace do '1+2+3+4+5'. scan(/([-+\/*])?(?:\s*)(\d+)/). reduce(0) { |res, (op, num)| res.public_send(op || :+, num.to_i) } end
Prints:
Method 1 event | line | id | classname ---------+------------+----------------------+---------------- line | 15 | | c-call | 15 | sum | Array c-return | 15 | sum | Array Method 2 event | line | id | classname ---------+------------+----------------------+---------------- line | 23 | | c-call | 22 | scan | String c-return | 22 | scan | String c-call | 23 | reduce | Enumerable c-call | 23 | each | Array line | 23 | | c-call | 23 | to_i | String c-return | 23 | to_i | String c-call | 23 | public_send | Kernel c-call | 23 | + | Integer c-return | 23 | + | Integer c-return | 23 | public_send | Kernel line | 23 | | c-call | 23 | to_i | String c-return | 23 | to_i | String c-call | 23 | public_send | Kernel c-call | 23 | + | Integer c-return | 23 | + | Integer c-return | 23 | public_send | Kernel line | 23 | | c-call | 23 | to_i | String c-return | 23 | to_i | String c-call | 23 | public_send | Kernel c-call | 23 | + | Integer c-return | 23 | + | Integer c-return | 23 | public_send | Kernel line | 23 | | c-call | 23 | to_i | String c-return | 23 | to_i | String c-call | 23 | public_send | Kernel c-call | 23 | + | Integer c-return | 23 | + | Integer c-return | 23 | public_send | Kernel line | 23 | | c-call | 23 | to_i | String c-return | 23 | to_i | String c-call | 23 | public_send | Kernel c-call | 23 | + | Integer c-return | 23 | + | Integer c-return | 23 | public_send | Kernel c-return | 23 | each | Array c-return | 23 | reduce | Enumerable
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
About performance, you might find this is interesting:
Prints: