-5

Here I am rephrasing the question. Coz some of the people couldnt understand it and started giving negative votes

I have the following PHP code:

$count = 100000;
$array = array();
for($i=0,$i<=$count,$i++)
{
    array_push($array,mt_rand(1111111111111111,9999999999999999));
}

As per current code the time to execute this code will increase as I increase $count.

I am looking for a solution / algo / technique by which I can keep the time to execute in seconds no matter whatever is the value for $count. I am running this on 8 cores cpu, I am OK changing language but not ok upgrading hardware.

Saurabh
  • 365
  • 8
  • 18
  • Whatever the solution, if you need more elements in *array*, that will take more time - unless you can avoid using an array, meaning changing your algorithm/implementation? – Déjà vu Nov 02 '13 at 05:57
  • You want to be able to execute an arbitrary function a million times and have it complete in the same time as just one? That's certainly daft and possibly nuts. –  Nov 02 '13 at 05:58
  • doesn't exist (to my knowledge), no matter what you have to iterate so... – mikakun Nov 02 '13 at 05:58
  • 6
    This question appears to be off-topic because it is asking for something impossible. –  Nov 02 '13 at 05:59
  • Thanks for quick comments @ring0 can you suggest some algos. – Saurabh Nov 02 '13 at 06:02
  • Can I run it as a threaded process? http://stackoverflow.com/questions/1532065/php-multithread – Saurabh Nov 02 '13 at 06:08
  • Could you explain why you think you need to do this? I think you are asking the wrong question here. – vascowhite Nov 02 '13 at 06:44
  • @vascowhite I want to generate large sets of random numbers and insert it into DB – Saurabh Nov 02 '13 at 07:21
  • why do u guys think this question is off topic it is a question which involves *a software algorithm *practical, answerable problems that are unique to software development – Saurabh Nov 02 '13 at 09:48

1 Answers1

2

This should give you approximately constant time for values of $count < $maxcount. However, I wouldn't ever do this - it is just timewasting.

$maxcount = 100000
$count = 100000;
$array = array();

// Create required array
for($i=0,$i<=$count,$i++)
{
    array_push($array,mt_rand(1111111111111111,9999999999999999));
}

// Pad out time to $maxcount iterations, to keep time constant
for($i=$count,$i<=$maxcount,$i++)
{
    $dummy = mt_rand(1111111111111111,9999999999999999);
}