This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Fastest way to calculate the size of an opened file

By: srakesh on Jul 13th, 2011  |  syntax: PHP  |  size: 0.86 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $fp = fopen("/tmp/temp.rock", "a+");
  3.  
  4. fwrite($fp, "This is the contents");
  5.  
  6. echo "Time taken to calculate the size by filesize function: ";
  7. $t = microtime(true);
  8. $ts1 = filesize("/tmp/temp.rock") . "\n";
  9. echo microtime(true) - $t . "\n";
  10.  
  11. echo "Time taken to calculate the size by fstat function:";
  12. $t = microtime(true);
  13. $ts1 = fstat($fp) . "\n";
  14. $size = $ts1["size"];
  15. echo microtime(true) - $t . "\n";
  16.  
  17. echo "Time taken to calculate the size by fseek and ftell function: ";
  18. $t = microtime(true);
  19. fseek($fp, 0, SEEK_END);
  20. $ts2 = ftell($fp) . "\n";
  21. echo microtime(true) - $t . "\n";
  22.  
  23. fclose($fp);
  24.  
  25. /**
  26. OUTPUT:
  27.  
  28. Time taken to calculate the size by filesize function: 2.4080276489258E-5
  29. Time taken to calculate the size by fstat function: 2.9802322387695E-5
  30. Time taken to calculate the size by fseek and ftell function: 1.2874603271484E-5
  31.  
  32. */
clone this paste RAW Paste Data