Why is this code so fast?

EDIT . This was due to an error in my code (possibly), after debugging and adding verification of the correct answer in my tests, the test did not find the difference (which annoys me), more in my own answer below. / EDIT

Hello,

I wrote myself a small CSS wrapper for SASS for PHP and programmed it to accept the file name and possible flags before running (and possibly cache, unless otherwise noted) my SASS file.

I also conducted several tests and versions of nr. 2 is something around 2x - 4x slower than the nr version. 1 , although version 1 should run more code, and then version 2 (it directly includes from the disk, and does not first parse the URL for the flags).

I do not understand why, in fact, the tests are somewhat too consistent to call it service data on the disk.

Here are the speed tests:

First, generate a file, then simply ask the cache for
Version 1 total: 10.886 s avg: 10.886 ms / file: 466.42 ms
Version 2 total: 21.235 s avg: 21.235 ms / file first: 14.54 ms

Just required from the cache
Version 1 total: 7.886 s avg: 7.886 ms / file first: 2.93 ms
Version 2: 21.657 s avg: 21.657 ms / file first: 6.98 ms

1:1: : 7.915 avg: 7.915 ms/file: 2.49 ms
2: 1: : 9.508 avg: 9.508 ms/file: 3.23 ms
1 2: : 1:17.137 avg: 7.714 ms/: 4.61 ms
2 run 2: total: 1:15.717 avg: 7.572 ms/: 2.69 ms * - 2 10000 .

1

/* HELPER FUNCTIONS */
function is_option($opt) { global $url_options; return in_array($opt,$url_options); }
function fail($message) { echo $message; die(); }

//prepare options array
$options=array();

$url_options = @explode('_',basename($_GET['f']));
if (!is_array($url_options))
    { fail('Wrong parameters given (or parameter can\'t be accepted)'); }
$loadfile = array_shift($url_options);

if (!file_exists('source/'.$loadfile.'.sass'))
{
    if (!file_exists('source/'.$loadfile.'.scss'))
        fail('Wrong parameters given (file doesn\'t exist)');
    else
        $options['property_syntax']='scss';
}else{
    $options['property_syntax']='sass';
}

$src_file = 'source/'.$loadfile.'.'.$options['property_syntax'];
$css_file = 'cache/'.$loadfile.'.css';

if (file_exists($css_file) && !is_option('no-cache'))
{
    header('content-type: text/css');
    require($css_file);
    die(); //ALL OK, loaded from cache
}

2

//quick! load from cache if exists!
if (file_exists('cache/'.($cachefile=basename('/',$_GET['f']))))
{
    header('content-type: text/css');
    require('cache/'.$cachefile);
    die(); //ALL OK, loaded from cache
}

/* HELPER FUNCTIONS */
function is_option($opt) { global $url_options; return in_array($opt,$url_options); }
function fail($message) { echo $message; die(); }

//prepare options array
$options=array();

$url_options = @explode('_',basename($cachefile));
if (!is_array($url_options))
    { fail('Wrong parameters given (or parameter can\'t be accepted)'); }
$loadfile = array_shift($url_options);

if (!file_exists('source/'.$loadfile.'.sass'))
{
    if (!file_exists('source/'.$loadfile.'.scss'))
        fail('Wrong parameters given (file doesn\'t exist)');
    else
        $options['property_syntax']='scss';
}else{
    $options['property_syntax']='sass';
}

$src_file = 'source/'.$loadfile.'.'.$options['property_syntax'];
$css_file = 'cache/'.$loadfile.'.css';

1, , , , v2 , ...

: , readfile , require, , , 1 ( 2 1000 10000, )

+3
3

, ​​ , ring0 ().

, , (n/10) - n , , :

1 ( ):
10000
4: 56,806[1292676882-1292677179]
: 29.681 miliseconds

1 ( ReadFile):
10000
4: 35,242[1292677437-1292677712]
: 27.524 miliseconds

2 ( ):
10000
4: 55,760[1292676879-1292677175]
: 29.576miliseconds

2 ( ReadFile):
10000
4: 32,336[1292677433-1292677706]
: 27.234 miliseconds

:
Speeds for 10,000 iteration calls

, / 2 (, /readfile ) , . , , , readfile ( Emil).

, , :)

+1

" 2 "?

2 , .

, " URL".

+1

There seems to be a mistake in

  if (file_exists('cache/'.($cachefile=basename('/',$_GET['f']))))

or

  • there is a type and explodethat should be used instead
  • or basenamenot used as it should - i.e. basename($_GET['f'])insteadbasename('/', $_GET['f'])

Therefore, it is $cachefileempty, ifalways true, and requireapplies to the directory cache.

+1
source

Source: https://habr.com/ru/post/1781008/


All Articles