Archive

Posts Tagged ‘profiling’

Java profiling

March 18, 2017 Leave a comment

YourKit is a great Java profiling tool. It is a commercial software but you can get a free 15-day evaluation license key for a fully functional version of the profiler.

The past week I was working on a Java project and the software was very slow. Using YourKit I could easily find the bottleneck. It turned out that 94% of the time was spent in a function that I implemented in a naive way. Choosing a better algorithm the software got much faster. A profiler is really useful…

For Java I use Eclipse. YourKit integrates in Eclipse perfectly through an Eclipse plugin.

Python profiling

Here is an excellent post about Python profiling.

Categories: java, python Tags: , ,

Measure execution time in PHP

November 23, 2010 Leave a comment

Problem

You want to measure the execution time of a piece of code.

Method #1

$start = (float) array_sum(explode(' ',microtime())); 
#	
// PHP code whose execution time you want to measure
#
$end = (float) array_sum(explode(' ',microtime()));
print "Processing time: ". sprintf("%.4f", ($end-$start))." seconds."; 

I found this tip here.

Method #2 (update 20110205)

There is a simpler solution; you can also write $start = microtime(true). See the PHP documentation here.

I use this method for simple profiling. For this, I use a class:

class Common
{
   /**
    * Get current microtime as a float. Can be used for simple profiling.
    */
   static public function get_microtime() {
      return microtime(true);
   }

   /**
    * Return a string with the elapsed time.
    * Order of $end and $start can be switched.
    */
   static public function elapsed_time($end, $start) {
      return sprintf("Elapsed time: %.4f sec.", abs($end - $start));
   }
}

Usage example:

$profile_start = Common::get_microtime();
// code to be profiled comes here
$profile_end = Common::get_microtime();
print Common::elapsed_time($profile_end, $profile_start);
Categories: php Tags: , , ,