PHP array key-value swap: performance comparison with other programming languages

王林
Release: 2024-04-30 12:48:01
Original
900 people have browsed it

PHP’s array_flip() function performs better than PHP on key-value swap tasks, but lags behind C, JavaScript, and Python. Specific benchmark results show: C took 0.000025 seconds, JavaScript took 0.000029 seconds, Python took 0.000032 seconds, and PHP took 0.000047 seconds.

PHP 数组键值互换:与其他编程语言的性能对比

PHP Array Key Value Swap: Performance Comparison with Other Programming Languages

Introduction

Array key-value swapping is a common operation in many programming languages, which involves swapping the keys and values ​​of an array. In PHP, this can be easily done using the array_flip() function. However, when talking about performance, it is helpful to compare it with other popular programming languages.

PHP

$array = ['a' => 1, 'b' => 2, 'c' => 3];

$flipped = array_flip($array);
Copy after login

Other programming languages

Python

array = {'a': 1, 'b': 2, 'c': 3}

flipped = {v: k for k, v in array.items()}
Copy after login

JavaScript

const array = {a: 1, b: 2, c: 3};

const flipped = Object.fromEntries(Object.entries(array).map(([k, v]) => [v, k]));
Copy after login

C (using std::unordered_map)

#include <unordered_map>

int main() {
  std::unordered_map<std::string, int> array = {{"a", 1}, {"b", 2}, {"c", 3}};

  std::unordered_map<int, std::string> flipped;
  for (auto const& [key, value] : array) {
    flipped[value] = key;
  }

  return 0;
}
Copy after login

Practical case

Assume you There is a JSON file containing the following data:

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}
Copy after login

To swap the key values, you can use the following code:

$json = json_decode($jsonString, true);
$flipped = array_flip($json);

var_dump($flipped);
Copy after login

Output:

array(3) {
  ["John Doe"]=>
  string(3) "name"
  [30]=>
  string(3) "age"
  ["New York"]=>
  string(4) "city"
}
Copy after login

Performance Test

To compare the performance of different languages, you can use a benchmarking framework. Below are the results of the benchmark test done using PHPBench:

##JavaScript0.000029C 0.000025##It can be seen from the results that for small inputs, C is the performance The best language, followed by JavaScript and Python. PHP performed slightly worse in this test.
Language Time (seconds)
PHP 0.000047
Python 0.000032

The above is the detailed content of PHP array key-value swap: performance comparison with other programming languages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template