Created
          November 11, 2016 14:34 
        
      - 
      
- 
        Save CodeBrauer/a73b85dae234774532cae468a1fc8fd9 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| $urls = [ | |
| 'http://www.flickr.com/services/feeds/photos_public.gne', | |
| 'https://i.stack.imgur.com/fgwQf.jpg', | |
| 'https://api.wordpress.org/secret-key/1.1/salt/', | |
| 'http://gabrielw.de', | |
| 'https://google.com', | |
| 'https://raw.githubusercontent.com/CodeBrauer/gmod-loadingscreen/master/img/bg.jpg', | |
| ]; | |
| $runs = 10; | |
| for ($i=1; $i <= $runs; $i++) { | |
| foreach ($urls as $url) { | |
| /** file_get_contents */ | |
| $start_fgc = microtime(true); | |
| echo "($i/$runs/#1) Getting ".str_pad(substr($url, 0, 30), 30, ' ')."\r"; | |
| $data = file_get_contents($url); | |
| $stop_fgc = microtime(true); | |
| $results[$url]['file_get_contents'][] = $stop_fgc - $start_fgc; | |
| /** curl */ | |
| $start_curl = microtime(true); | |
| $ch = curl_init(); | |
| $options = [ | |
| CURLOPT_SSL_VERIFYPEER => false, | |
| CURLOPT_RETURNTRANSFER => true, | |
| CURLOPT_URL => $url, | |
| ]; | |
| echo "($i/$runs/#2) Getting ".str_pad(substr($url, 0, 30), 30, ' ')."\r"; | |
| curl_setopt_array($ch, $options); | |
| $data = json_decode(curl_exec($ch)); | |
| curl_close($ch); | |
| $stop_curl = microtime(true); | |
| $results[$url]['curl'][] = $stop_curl - $start_curl; | |
| } | |
| } | |
| echo PHP_EOL . '== RESULTS =====================================' . PHP_EOL; | |
| echo "URL | fgc | curl" . PHP_EOL; | |
| foreach ($results as $url => $results) { | |
| $avg_curl = number_format( array_sum($results['curl']) / count($results['curl']), 4 ); | |
| $avg_fgc = number_format( array_sum($results['file_get_contents']) / count($results['file_get_contents']), 4 ); | |
| echo str_pad(substr($url, 0, 30), 30, ' ') . " | $avg_curl | $avg_fgc" . PHP_EOL; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment