0

I want to concatenate two similar range (for unicode number conversion purpose),to make an array like this:

   $nums = [
    0=> 0,
    1=> 1,
    2=> 2,
    3=> 3,
    4=> 4,
    5=> 5,
    6=> 6,
    7=> 7,
    8=> 8,
    9=> 9,
    10=> 0,
    11=> 1,
    12=> 2,
    13=> 3,
    14=> 4,
    15=> 5,
    16=> 6,
    17=> 7,
    18=> 8,
    19=> 9
    ];

I can do this simply with concatenating like this code:

$nums=array_merge(range(0,9),range(0,9));

But I am looking for more efficient or a dedicated PHP function for this job. I will appriciate any kind of method or mention dedicated function existence.

7
  • More efficient is C Commented Jul 2, 2016 at 13:46
  • 2
    You could have that in a function, like so: https://eval.in/599535. That way you can select the start, the end and the quantity easily. Commented Jul 2, 2016 at 13:49
  • I don't know what your goal is, but it sounds like you need the Modulo operator, not any functions. Commented Jul 2, 2016 at 14:39
  • It is part of unicode numbers(numbers not in english) converter. Commented Jul 2, 2016 at 15:26
  • @FirstOne +1 both for your code and eval.in Presentation. Commented Jul 2, 2016 at 19:35

1 Answer 1

1

Update (because version was not reliably reproducibly faster than OP's original code.)

Does it have to be an array merge? Based on your answer to my comment above, i can assume that the 2 ranges are always the same. Ie. its just a repeating range / sequence.

You also mention, that it is for conversion, so i guess its a lookup table.

$input = 11;
$nums = array_merge(range(0,9),range(0,9))
echo $nums[$input];

// This will output 1

Assuming this is the purpose, giving it eg. 11 as input and getting the 1 for result, you can gain much speed by doing this without arrays.

Assume that range is 0, 9, and 11 again as input:

$input = 11;

$rmin = 0;
$rmax = 9;

$rmax = $rmax + 1 - $rmin;
$output = ($input % $rmax) + $rmin;   

echo $output;

// This will output 1

the $input => $output values will match your array version excactly.You can also rmin and rmax to match whatever range you want.

Speed-wise its a little hard to compare because its 2 different approaches, but 100.000 times of

nums = array_merge(range(0,9),range(0,9));

is 0.272 seconds here, and 100.000 rounds of my code above (without the "echo") gives 0.021 seconds.

Sign up to request clarification or add additional context in comments.

11 Comments

+1 : for your time and algorithm. I check both your code and $nums = array_merge(range(0,9),range(0,9)); with 100,000 iterations . my code takes 0.20 sec and yours takes 0.25 sec.(as your awareness) and simply assigning 20 elements array in that itterations takes just about half(0.089 sec)
In addition to my last comment ,I think we should not calculate time after main algorithm, specially print function is more time consuming and can make big tolerances.If you make second microtime() before print function , you will get more noticeable results
I just tested using $total = microtime(1); print "new: ".$total."\n"; instead. Makes no difference at all.
I tested both codes on my pc and you can see the test code and my outputs on https://eval.in/599867
Your code from eval.in gives me, on my computer: 0.26929116249084 0.34282088279724 0.2298538684845
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.