Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-Type: text/plain');
- $json_string = '[{"highest_education":"B.E ( Automobile Engineering )","occupation":"Job in Private Office","annual_income":""}]';
- function method1($json_string) {
- $json_array = json_decode($json_string, true);
- if(isset($json_array['annual_income']) && !empty($json_array['annual_income'])) {
- // value is not null
- } else {
- // value is null or empty
- }
- }
- function method2($json) {
- $annual_income1='"annual_income":""';
- $annual_income2='"annual_income":null';
- if(strpos($json,$annual_income1) !== false || strpos($json,$annual_income2) !== false) {
- // value is null or empty
- }
- }
- $start = microtime(true);
- for($i = 0; $i < 1000; $i++) {
- method1($json_string);
- }
- $end = microtime(true);
- $elapsed_mehtod1 = round($end - $start, 6);
- echo 'Method 1 used: ' . $elapsed_mehtod1 . 's' . PHP_EOL;
- $start = microtime(true);
- for($i = 0; $i < 1000; $i++) {
- method2($json_string);
- }
- $end = microtime(true);
- $elapsed_mehtod2 = round($end - $start, 6);
- echo 'Method 2 used: ' . $elapsed_mehtod2 . 's' . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement