33

Experimenting with Ruby's Benchmark module...

>> Benchmark.bm(7) { |b| b.report('Report:') { s = '' ; 10000.times { s += 'a' } }  }
             user     system      total        real
Report:  0.150000   0.010000   0.160000 (  0.156361)

What are the meanings of "user", "system", and "real"?

Ethan
  • 57,819
  • 63
  • 187
  • 237

2 Answers2

62

These are the same times that the Unix time command or other typical benchmarking tools would report:

  • user: the amount of time spent executing userspace code (i.e.: your code),
  • system: the amount of time spent executing kernel code and
  • real: the "real" amount of time it took to execute the code (i.e. system + user + time spent waiting for I/O, network, disk, user input, etc.). Also known as "wallclock time".
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • @jorg Are the Ruby docs for Benchmark erroneous in that they show real times that are much shorter than total times? Source: http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html – rscott Aug 28 '12 at 14:27
  • 7
    @rscott: user, system and total refer to the sum of time across all cores, whereas real is just the literal total time. Hence if code is running on a multi-core system, any of the first three can exceed the latter. – Sparhawk Mar 12 '13 at 23:59
  • 3
    How about "total" then? I have a benchmark where "total" exceeds the sum of "user" + "system". How's that possible? – troelskn Nov 27 '14 at 12:44
-5

Please check this gem: https://github.com/igorkasyanchuk/benchmark_methods

No more code like this:

t = Time.now
user.calculate_report
puts Time.now - t

Now you can do:

benchmark :calculate_report # in class

And just call your method

user.calculate_report
Igor Kasyanchuk
  • 766
  • 6
  • 12