PHP, 벤치마크 시간
벤치마크 어레이와 객체
이 벤치마크는 다음 기능을 테스트합니다.
그리고 사용된 변수의 예입니다.
$array_numeric=[$hello,$second,$third];
$array_not_numeric=['hello'=>$hello,'second'=>$second,'third'=>$third];
$object_constructor=DummyClass('world',0,20.3);
$object_no_constructor=new DummyClass2();
$object_no_constructor->hello='world';
$object_no_constructor->second=0;
$object_no_constructor->third=20.3;
또한 팩토리, 생성자, setter 및 getter, stdClass 및 매직 메서드의 사용을 테스트합니다.
공장이란? 팩토리는 엔티티(이 경우 배열)를 생성하는 데 사용되는 함수입니다.
생성자는 무엇입니까? 생성자는 클래스의 일부이며 개체의 인스턴스를 초기화하는 데 사용됩니다.
결과(작을수록 좋음)
몇 초 만에 벤치마크 결과입니다.
배열 숫자 공장 없음
어레이 공장 없음
배열 숫자 팩토리
어레이 공장
객체 생성자
객체 생성자 없음
객체 생성자 setter/getter 없음
객체 생성자 setter/getter 없음(마법)
객체 생성자 없음 stdClass
0.038275957107543945
0.04024696350097656
0.12892484664916992
0.15126800537109375
0.12696218490600586
0.08770990371704102
0.21163702011108398
0.3990211486816406
0.13244986534118652
더 작은 결과와 비교한 백분율로 결과입니다.
배열 숫자 공장 없음
어레이 공장 없음
배열 숫자 팩토리
어레이 공장
객체 생성자
객체 생성자 없음
객체 생성자 setter/getter 없음
객체 생성자 setter/getter 없음(마법)
객체 생성자 없음 stdClass
0%
5.15%
236.83%
295.2%
231.7%
129.15%
452.92%
942.49%
246.04%
결론:
ps. 테스트는 100만번 실행했는데 그 차이가 0.3초도 안되는데 그게 중요한가요?
100명의 동시 사용자가 있고(작은 수는 아니지만 불가능하지는 않음) 1000개의 값이 있는 목록을 처리하고 반환한다고 가정해 보겠습니다. 100x1000 = 100,000개 개체입니다. 따라서 100,000개의 개체를 고려하면 그 차이는 최악의 경우 0.03초 미만입니다. 그러나 우리 시스템은 단일 작업 이상을 처리하므로 개체 목록을 표시하는 경우 유효성 검사, 다른 값 표시, 유효성 검사, 데이터베이스에서 읽고 메모리에 저장하고 웹 또는 직렬화되므로 이 값은 상당할 수 있으며 CPU 사용은 다른 프로세스의 맨 위에 있습니다. 작은 프로젝트에는 큰 문제가 아니지만 큰 프로젝트에는 중요합니다.
tl/dr
$customer[0]='john'; // faster but it is hard to understand
$customer['name']='john'; // almost as fast as the first one but it is clear to understand (it also uses more memory)
$customer->name='john'; // (where $customer is an object of the class Customer) slower but it still acceptable.
$customer->name='john'; // (where $customer is an object of the class stdClass) the double of slower than to use a class.
$customer=new Customer('john'); // (constructor) even slower but is still acceptable;
$customer=factory('john'); // (where factory is an array that returns an array). Slower than the use of constructor.
$customer->setName('john'); // bad performance
$customer->name='john'; // (where the class uses a magic method) awful performance, avoid this one.
코드는 여기에 있습니다(테스트하려는 경우)
https://github.com/EFTEC/php-benchmarks/blob/master/benchmark_array_vs_object.php
Reference
이 문제에 관하여(PHP, 벤치마크 시간), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/jorgecc/php-benchmark-time-1j76텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)