Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

40. Benchmarking > The Benchmark Module

The Benchmark Module

Now I want to rewrite my previous examples using Benchmark. To record a time, I construct a Benchmark object. The constructor creates a list of the times returned by time and times, although I don’t need to worry about that, since I just use the object-oriented interface.

	use Benchmark;



	my $start_time = new Benchmark;



	# My code here



	my @array = (1 .. 1000000);

	foreach my $element ( @array ) { $_ += $element }



	my $end_time = new Benchmark;

I also need a way to determine the time difference, which I can do with the timediff function:

	my $difference = timediff($end_time, $start_time);

This returns another Benchmark object. When I want to see the times that I have measured, I use the timestr method:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint