Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哪一个最好?PHP lambda vs类对象方法_Php_Lambda_Anonymous - Fatal编程技术网

哪一个最好?PHP lambda vs类对象方法

哪一个最好?PHP lambda vs类对象方法,php,lambda,anonymous,Php,Lambda,Anonymous,想知道以下方法的性能有什么不同 // in dependencies.php $greeting=function(){echo "lambda";}; // in MyClass.php class MyClass{ function greeting(){echo "class";} } // in index.php include dependencies.php include MyClass.php .... // assume using Slim or Laravel r

想知道以下方法的性能有什么不同

// in dependencies.php
$greeting=function(){echo "lambda";};

// in MyClass.php
class MyClass{
 function greeting(){echo "class";}
}

// in index.php
include dependencies.php
include MyClass.php

....
// assume using Slim or Laravel routing. 
$app->get('/test1', $greeting);
$app->get('/test2', 'MyClass:greeting');
$app->get('/test3', function(){ echo "anonymous"; });

哪种方法性能最好?为什么?

您可以使用此方法查看运行每个脚本需要多少时间

$start =  microtime(true);
$app->get('/test1', $greeting); // replace this one with each of the others
$end = microtime(true);
echo $end-$start; // microseconds
至于内存:

echo memory_get_usage() . "\n"; 
$app->get('/test1', $greeting); // replace this one with each of the others
echo memory_get_usage() . "\n"; 
但我相信您可以找到合适的基准库

在我个人看来,课堂教学法可能是最慢的。这可能是由于大型构造函数或析构函数的实现。

我已经尝试过了

// in dependencies.php
$greeting=function(){echo "lambda";};
$greeting2=function(){echo "lambda2";};

// in MyClass.php
class MyClass{
 function greeting(){echo "class";}
}

// in MyClass2.php
class MyClass2{
 function greeting(){echo "class2";}
}

// in index.php
include dependencies.php
include MyClass.php
include MyClass2.php

....
// I test this with Slim3 framework
$app->get('/test1', $greeting);
$app->get('/test2', $greeting2);
$app->get('/test3', 'MyClass:greeting');
$app->get('/test4', 'MyClass2:greeting');
$app->get('/test5', function(){ echo "anonymous"; });
结果是: 测试1 0.0031208992004395 测试2 1.8119812011719E-5 测试3 1.0967254638672E-5 测试4 1.001358032266E-5 测试5 1.0967254638672E-5 然后我尝试了不同的序列,结果如下: 测试1 0.0039870738983154 测试3 1.9073486328125E-5 测试2 1.5020370483398E-5 测试4 1.0967254638672E-5 测试5 1.2159347518E-5 测试1 0.004763126373291 测试5 2.1934509277344E-5 测试3 1.1920928955078E-5 测试2 1.0967254638672E-5 测试4 1.001358032266E-5 测试5 0.0038559436798096 测试1 2.0027160644531E-5 测试3 1.2874603271484E-5 测试2 1.1920928955078E-5 测试4 1.0967254638672E-5
在这个简单的实现中,看起来它是lambda、object还是anonymous实际上并不重要

如果您对性能感兴趣:对其进行基准测试。很可能这根本没有实际意义。。。。在这样短的代码中,解决更快的问题或占用更少的内存是没有意义的。是的,但是假设函数会变得更复杂一点,它可能会变得有用。我只希望对更长的代码这样做,而不是对这样短的代码。但你是对的。在这个简短的例子中,它是对的。代码越长,效果越好。 The results: test1 0.0031208992004395 test2 1.8119812011719E-5 test3 1.0967254638672E-5 test4 1.0013580322266E-5 test5 1.0967254638672E-5 then i tried different sequences, here the results: test1 0.0039870738983154 test3 1.9073486328125E-5 test2 1.5020370483398E-5 test4 1.0967254638672E-5 test5 1.215934753418E-5 test1 0.004763126373291 test5 2.1934509277344E-5 test3 1.1920928955078E-5 test2 1.0967254638672E-5 test4 1.0013580322266E-5 test5 0.0038559436798096 test1 2.0027160644531E-5 test3 1.2874603271484E-5 test2 1.1920928955078E-5 test4 1.0967254638672E-5