| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 | $methods = array( 'my_data_clone', 'wrapped_test', 'unserser', 'shallowcopy');
|
|---|
| 5 | $count = 1000000;
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | echo "Number of iterations per: $count\n";
|
|---|
| 9 |
|
|---|
| 10 | $a = (string) 'Some simple string';
|
|---|
| 11 | run_tests( 'string', $a );
|
|---|
| 12 |
|
|---|
| 13 | $a = (integer) 1234567;
|
|---|
| 14 | run_tests( 'integer', $a );
|
|---|
| 15 |
|
|---|
| 16 | $a = (float) 123456.78;
|
|---|
| 17 | run_tests( 'float', $a );
|
|---|
| 18 |
|
|---|
| 19 | $a = new StdClass();
|
|---|
| 20 | $a->test = 'the';
|
|---|
| 21 | $a->test2 = 'the';
|
|---|
| 22 | $a->test3 = 'the';
|
|---|
| 23 |
|
|---|
| 24 | $new = run_tests( 'stdclass', $a, 'unset($new->test2);' );
|
|---|
| 25 |
|
|---|
| 26 | $a = array();
|
|---|
| 27 | $a['test'] = 'the';
|
|---|
| 28 | $a['test2'] = 'the';
|
|---|
| 29 | $a['test3'] = 'the';
|
|---|
| 30 | run_tests( 'array', $a, 'unset( $new["test2"] );');
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | $a = array();
|
|---|
| 34 | $a['test'] = new StdClass();
|
|---|
| 35 | $a['test']->test = 'the';
|
|---|
| 36 | $a['test']->test2 = 'the';
|
|---|
| 37 | $a['test']->test3 = 'the';
|
|---|
| 38 | run_tests( 'array of obj', $a, 'unset( $new["test"]->test2 );');
|
|---|
| 39 |
|
|---|
| 40 | $a = array();
|
|---|
| 41 | $a['test'] = array();
|
|---|
| 42 | $a['test']['leveltwo'] = new StdClass();
|
|---|
| 43 | $a['test']['leveltwo']->test = 'something';
|
|---|
| 44 | $a['test']['leveltwo']->test2 = 'something';
|
|---|
| 45 | $a['test']['leveltwo']->test3 = 'something';
|
|---|
| 46 |
|
|---|
| 47 | $a['test']['leveltwoa'] = new StdClass();
|
|---|
| 48 | $a['test']['leveltwoa']->test = 'something';
|
|---|
| 49 | $a['test']['leveltwoa']->test2 = 'something';
|
|---|
| 50 | $a['test']['leveltwoa']->test3 = 'something';
|
|---|
| 51 | run_tests( 'array of array of obj', $a, 'unset( $new["test"]["leveltwo"]->test2);' );
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | function run_tests( $name, $what, $changer = null ) {
|
|---|
| 55 | global $methods;
|
|---|
| 56 | global $count;
|
|---|
| 57 | foreach ($methods as $method) {
|
|---|
| 58 | $start = microtime( true );
|
|---|
| 59 | $new = run_test( $method, $what, $count);
|
|---|
| 60 | $stop = microtime( true );
|
|---|
| 61 |
|
|---|
| 62 | echo "Total ($name $method): " . number_format(($stop - $start), 5) . " sec\n";
|
|---|
| 63 |
|
|---|
| 64 | $old_b4_json = json_encode( $what );
|
|---|
| 65 | if (isset( $changer ) ) {
|
|---|
| 66 | eval( $changer );
|
|---|
| 67 |
|
|---|
| 68 | }
|
|---|
| 69 | $old_after_json = json_encode( $what );
|
|---|
| 70 | $new_json = json_encode( $new );
|
|---|
| 71 | if ( $old_b4_json !== $old_after_json ) {
|
|---|
| 72 | echo "old_b4 !== old_after ($name) ($method)\n";
|
|---|
| 73 | var_dump( $old_b4_json );
|
|---|
| 74 | var_dump( $old_after_json );
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | function run_test( $method, $what, $count ) {
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | for ($x = 0; $x < $count; $x++ ) {
|
|---|
| 84 | $new = call_user_func( $method, $what );
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return $new;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | function shallowcopy( $what ) {
|
|---|
| 92 | $new = $what;
|
|---|
| 93 | return $new;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | function unserser( $what ) {
|
|---|
| 97 | return unserialize( serialize( $what ) );
|
|---|
| 98 |
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | // This was the my first attempt at the deep copy and it turned out to be the fastest
|
|---|
| 102 | function my_data_clone( $what ) {
|
|---|
| 103 | if ( is_object( $what ) ) {
|
|---|
| 104 | return clone $what;
|
|---|
| 105 | } elseif ( is_array( $what ) ) {
|
|---|
| 106 | $new = array();
|
|---|
| 107 | foreach ( $what as $key => $value ) {
|
|---|
| 108 | if ( is_object( $value ) ) {
|
|---|
| 109 | $new[ $key ] = clone $value;
|
|---|
| 110 | } elseif ( is_array( $value ) ) {
|
|---|
| 111 | $new[ $key ] = my_data_clone( $value );
|
|---|
| 112 | } else {
|
|---|
| 113 | $new[ $key ] = $value;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | }
|
|---|
| 117 | return $new;
|
|---|
| 118 | } else {
|
|---|
| 119 | return $what;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | return null;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | // This is closer to what WP would do since the cache setter already does is_object
|
|---|
| 126 | function wrapped_test( $what ) {
|
|---|
| 127 | if ( is_object( $what ) ) {
|
|---|
| 128 | return clone $what;
|
|---|
| 129 | } elseif ( is_array( $what ) ) {
|
|---|
| 130 | return my_data_clone( $what );
|
|---|
| 131 | }
|
|---|
| 132 | return $what;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|