1

For example you have a number like "11956576835". If you try nth digit from a number like this, you can simply:

echo substr("11956576835",3,1);

But for the performance is it better to use a math function like this for performance?

echo (11956576835/pow(10,0))%10;

Thank you all for any response...

Update: thanks to marcus, the fastest solution to access a string as an array. http://codepad.viper-7.com/v8elnt i.e:

$number = "11956576835";
echo $number[3];
Marcus
  • 12,296
  • 5
  • 48
  • 66
Midori Kocak
  • 121
  • 1
  • 7
  • 1
    Can I ask why you want to know this? how does it impact what you're doing? – Darren Oct 13 '14 at 06:30
  • 1
    Why don't you try to measure it? record the start time, execute each method a number of times (one milion for example). subtract the start time from the current time and you know. – Ikke Oct 13 '14 at 06:30
  • Since you seem to only need one digit you could also take advantage of that a string is an array of chars. E.g. `"11956576835"[3]` would return `5` – Marcus Oct 13 '14 at 06:40
  • @Marcus "11956576835"[3] is it better from substr()? – Midori Kocak Oct 13 '14 at 06:51
  • Here's a quick benchmark: http://codepad.viper-7.com/2Z8GGc. It shows that index based getter is faster, while the difference between substr and math is negligible – Marcus Oct 13 '14 at 06:51
  • @Darren I was just curious. I am writing a class to validate turkish id numbers that has a digit algorithm. It will be open source, I wil post it when I finish it. – Midori Kocak Oct 13 '14 at 06:51
  • @lkke Yeah you are right.. – Midori Kocak Oct 13 '14 at 06:53

1 Answers1

2

Using substr is faster than using a math function for the simple reason that there is lesser processing required in the first call. The second call has a function invocation, power and modulus calculations. The first one just retrieves a number from a sequence position.

The difference however would be negligible unless you are a certain someone who goes by the moniker of flash :)

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • another question: substr() or $string[digit]? – Midori Kocak Oct 13 '14 at 06:48
  • I would go with $string[digit] - since it is just offsetting, as opposed to substr() which again has the overhead of a function call. However the difference, again, would be very very miniscule. – raidenace Oct 13 '14 at 06:52
  • If you use an int instead of string, the lesser processing is no longer true. Extended example of @Marcus http://codepad.viper-7.com/SOGip0 – andy Oct 13 '14 at 06:59
  • 1
    @andy Theoretically, I would imagine it to be slower considering that there are more processing instructions required. Having said that there are specialized architecture in many of today's processors to make number crunching faster. If we are going to get down to that level of specifics, we would need to check what kind of server/processing capabilities the codepad.viper server has. I would go with the simple rule of thumb that if the differences are in sub second level,just go with the one that has least number of processing instructions. – raidenace Oct 13 '14 at 07:06
  • Processors where always specialized for crunching numbers. My rule of thumb when it comes to performance is that string processing is slow. Maybe this becomes more clear, if we only consider the actual math version, not taking into account the `pow()` which is fixed anyways. See: http://codepad.viper-7.com/WYLT1i – andy Oct 13 '14 at 07:21