'What is faster in PHP: dechex() or sprintf("%x")?

What is the fastest way to convert a number to the hexadecimal representation in PHP: dechex($number) or sprintf("%x",$number)?



Solution 1:[1]

Here you go... dechex is faster, by an insignifficant number
0.0042848587036133
0.0037119388580322

<?php

$numbers = [];

for ($i = 1; $i < 10000; $i++) {
    $numbers[] = random_int(1, 10000);
}

$start = microtime(true);
foreach ($numbers as $number) {
    $test = sprintf("%x",$number);
}

echo microtime(true) - $start . PHP_EOL;

$start = microtime(true);
foreach ($numbers as $number) {
    $test = dechex($number);
}

echo microtime(true) - $start . PHP_EOL;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guido Faecke