Coder Social home page Coder Social logo

php-bench's People

Contributors

nafigator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

php-bench's Issues

Inverted tests -> inverted results

I received a link to this repo from a colleague as an evidence using (int) (type cast) is faster than using intval(...).

I ran the scripts with only this addition:

$ diff test test-example 
64d63
< $application->setRepeats($argv[2] ?? 10000);

So I could run tests with an arbitrary repeat number.

Running the included IntvalVsTypeCast I got these results:

$ ./test IntvalVsTypeCast 1000000
# omitted progress bars for brevity
Test name       Repeats         Result          Performance     
(int)           1000000         0.115288 sec    +0.00%
intval()        1000000         0.120689 sec    -4.68%

Which seems to validate my colleague assumption.

But then I created a new class and just inverted the tests (code at end) and got inverted results:

$ ./test TypeCastVsIntval 1000000
# omitted progress bars for brevity
Test name       Repeats         Result          Performance     
intval()        1000000         0.137540 sec    +0.00%
(int)           1000000         0.143018 sec    -3.98%

Of course the difference is not big enough, and convinced this colleague to stop complaining on me using intval() instead of (int) (also from the progress bar output it seems intval() uses slightly less memory, but it is very unlikely one would need to run a million integer conversion in a row), but I thought it would be a good idea to report.

I didn't run other tests to check if this issue holds true for all of them. And of course, I ran both tests several times before posting this issue, and the results don't vary very much.

My system reference:

$ neofetch --backend off
rodrigo@rodrigo-pc 
------------------ 
OS: KDE neon User Edition 5.21 x86_64 
Kernel: 5.4.0-73-generic 
Uptime: 2 hours, 32 mins 
Packages: 2694 (dpkg), 15 (flatpak), 10 (snap) 
Shell: bash 5.0.17 
Resolution: 1920x1080, 1920x1080, 1920x1080, 1920x1080 
DE: Plasma 
WM: KWin 
WM Theme: plastik 
Theme: Breeze Light [Plasma], Breeze [GTK2/3] 
Icons: breeze [Plasma], breeze [GTK2/3] 
Terminal: konsole 
Terminal Font: Ubuntu Mono 14 
CPU: Intel i9-9900 (16) @ 3.100GHz 
GPU: NVIDIA GeForce GTX 1650 
Memory: 6437MiB / 64264MiB 

PHP version:

$ php -v
PHP 7.4.18 (cli) (built: May  3 2021 11:27:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.18, Copyright (c), by Zend Technologies

Added class:

<?php
/**
 * What is faster typecasting or function intval()
 *
 * @file      IntvalVsTypeCast.php
 *
 * PHP version 5.4+
 *
 * @author    Alexander Yancharuk <alex at itvault dot info>
 * @date      Thu Apr 03 14:39:45 2014
 * @license   The BSD 3-Clause License
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
 */

namespace Tests;

use Application\TestApplication;
use Veles\Tools\CliProgressBar;
use Veles\Tools\Timer;

/**
 * Class TypeCastVsIntval
 *
 * @author Alexander Yancharuk <alex at itvault dot info>
 */
class TypeCastVsIntval extends TestApplication
{
    protected $repeats = 10000;

	public function run()
	{
		$repeats = $this->getRepeats();

		$bar = new CliProgressBar($repeats);
		for ($i = 1; $i <= $repeats; ++$i) {
			Timer::start();
			intval(5.55);
			Timer::stop();
			$bar->update($i);
		}

		$this->addResult('intval()', Timer::get());

		Timer::reset();
		$bar = new CliProgressBar($repeats);
		for ($i = 1; $i <= $repeats; ++$i) {
			Timer::start();
			(int) 5.55;
			Timer::stop();
			$bar->update($i);
		}

		$this->addResult('(int)', Timer::get());
	}
}

Note: I just inverted the tests from IntvalVsTypeCast, although in both classes the tests don't match the class name order.

If I can provide any other info to help debug it, please let me know.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.