title: How to get execution time and memory usage of a program (c,c++,java,python,php) in php script on ubuntu?
body: <p>I am trying to build an online judge website using php with laravel which can judge an user's submitted code for a problem. The server environment is ubuntu.</p>
  
  <p>Each problem has an input file and corresponding output file and maximum execution time limit and memory usage limit for the code.</p>
  
  <p>Suppose, for a problem input file is input.in and output file is correct.out. The judge will run the user's code and generate output file and compare it with correct.out to check whether the solution is correct. It also will check whether the code ran in the given time limit and memory limit.  </p>
  
  <p>Users are allowed to submit their solutions using c, c++, java, python or php</p>
  
  <p><strong>What I have done:</strong></p>
  
  <p>For a c++ code, suppose user's code is code.cpp. I have used exec() in my php script like this:</p>
  
  <pre><code>exec('g++ -o code code.cpp');
  
  </code></pre>
  
  <p>This creates an executable file named 'code'.</p>
  
  <p>Now, I have run the executable with the input.in file and generated the the output.out file by using:</p>
  
  <pre><code>exec('./code &lt; input.in &gt; output.out');
  </code></pre>
  
  <p>This gives the output file.</p>
  
  <p>Now I need to know how much time the program took to execute and how much memory it used for running or declaring variable, array or other data structures inside the code.</p>
  
  <p>I can find the execution time by calculating the difference between start time and end time of the execution. </p>
  
  <p><strong>My questions are:</strong> </p>
  
  <ul>
  <li><p>Is there any better way to find execution time?</p></li>
  <li><p>How to find how much the memory the program used?</p></li>
  <li><p>How to check for compilation error, runtime error?</p></li>
  </ul>