PHP, 벤치마크 시간

6394 단어 phpbenchmark

벤치마크 어레이와 객체



이 벤치마크는 다음 기능을 테스트합니다.
  • 변수를 생성합니다.
  • 그런 다음 간단한 값을 읽습니다
  • 그리고 목록에 추가합니다(목록은 라운드마다 생성됨)

  • 그리고 사용된 변수의 예입니다.

    $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%


    결론:
  • 숫자 배열과 연관 배열의 차이는 불과 5%에 불과하므로 동일하다고 할 수 있습니다.
  • 개체를 사용하면 +100% 느려지지만 대부분의 조건에서 여전히 허용됩니다(일명 두 배의 시간 사용).
  • 메서드를 호출하거나 생성자를 사용하면 값이 상당히 증가합니다. 또한 배열/팩토리보다 객체/생성자를 사용하는 것이 좋습니다. 왜요? 모르겠어요.
  • setter/getter를 사용하면 성능에 상당한 영향을 미칩니다. 할 수 있다면 그것을 피해야 합니다.
  • 매직 세터와 게터를 사용하는 것은 끔찍합니다(거의 10배 느림). Laravel이 느린 이유는 무엇입니까?
  • 또한 setter와 getter는 기본적이며 다른 유효성 검사 필드가 있는지 확인하지 않습니다.

  • 그리고 stdClass(익명 클래스)를 사용하는 것도 나쁘지만 setter와 getter를 사용하는 것만큼 나쁘지는 않습니다.

  • 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

    좋은 웹페이지 즐겨찾기