Hi,
i've been always used the in_array() function to check availabylity of a
value in array. Until today. Simple code: (do not run it tho :) )
<?
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
$first=range("a","z");
$second=range("a","z");
$third=range("a","z");
$all_arr=array();
foreach ($first as $f){
foreach ($second as $s){
foreach ($third as $t){
$ind=$f.$s.$t;
if (!in_array($ind,$all_arr)) $all_arr[]=$ind;
}
}
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo $time;
?>
It prints the time script worked. Guess the number?
250(!!!!) seconds!!!
Is there any alternative in PHP to check array?
--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk 6 3270
Berimor wrote: It prints the time script worked. Guess the number? 250(!!!!) seconds!!! Is there any alternative in PHP to check array?
There is array_search, but I didn't benchmark it.
HTH.
Peter.
-- http://www.phpforums.nl
On Sun, 04 Dec 2005 16:36:43 +0100, Peter van Schie
<va************@gmail.com> wrote: Berimor wrote:
It prints the time script worked. Guess the number? 250(!!!!) seconds!!! Is there any alternative in PHP to check array? There is array_search, but I didn't benchmark it.
i did - the same.
HTH. Peter.
--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
The hash table was invented precisely to solve this sort of problem.
Example:
$first=range("a","z");
$second=range("a","z");
$third=range("a","z");
$all_hash=array();
foreach ($first as $f){
foreach ($second as $s){
foreach ($third as $t){
$all_hash[$f.$s.$t] = true;
}
}
}
$all_arr = array_keys($all_hash);
On 04/12/2005 15:34, Berimor wrote:
[snip] $first=range("a","z"); $second=range("a","z"); $third=range("a","z"); $all_arr=array();
foreach ($first as $f){ foreach ($second as $s){ foreach ($third as $t){ $ind=$f.$s.$t; if (!in_array($ind,$all_arr)) $all_arr[]=$ind; } } }
[snip]
It prints the time script worked. Guess the number? 250(!!!!) seconds!!!
I'm not that surprised, though it took just less than a minute here. Do
you understand how much work you're expecting PHP to do?
In each iteration of the innermost loop, 'ind' will be assigned a unique
value. The in_array function must then search 'all_arr' and attempt to
find it. As it will never exist, the function will perform an exhaustive
search, eventually returning false.
In the first iteration, 'all_arr' will be empty. In the second, it will
have one element. By the final iteration, 'all_arr' will contain 17,575
elements (the final one yet to be added). To reach, and finally execute,
the final assignment, the in_array function will have performed
154,449,100 string comparisons (the sum of the number of elements on
each iteration) without a single match. The rest of the code will have
its own overheads, too.
Is there any alternative in PHP to check array?
It would seem that a better algorithm is what you really need. On this
machine, removing the if statement reduced the execution time to about
60ms: approximately 93 times faster.
Why do you think you need to keep checking the values in the array? If
you explain what you're doing, someone may be able to show you an
alternative approach.
Mike
--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Berimor wrote: Is there any alternative in PHP to check array?
foreach ($first as $f){
foreach ($second as $s){
foreach ($third as $t){
$all_arr[] = $f.$s.$t;
}
}
}
$all_arr = array_unique($all_arr);
JW
Berimor wrote: Hi, i've been always used the in_array() function to check availabylity of a value in array. Until today. Simple code: (do not run it tho :) )
<snip> It prints the time script worked. Guess the number? 250(!!!!) seconds!!! Is there any alternative in PHP to check array?
IIRC, Ilia Alshanetsky, one of the PHP core developer and maintainer
blogged on this some time ago. According to him, isset() will be the
fastest; but for isset(), you need key.
FWIW, it's nice to see many people have now become optimization
advocates recently in c.l.php--a very rarest gesture some years ago:)
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: AJ Z |
last post by:
I am using in_array() to search for a value ("other"), in order to validate
a form.
If I pass $_POST as the array to search PHP says that it is an
invalid datatype.
It is an array and if I copy...
|
by: Mountain Man |
last post by:
Hi,
The in_array function sometimes malfunctions on my computer yielding
the error message shown below. The code shown below usually works, but
sometimes doesn't. This even happens with the same...
|
by: Phil Powell |
last post by:
if (is_array($_POST)) {
foreach ($this->getAssocSectionsObjArray($key, $dbAP) as
$obj) {
print_r($obj); print_r(" in array? ");
print_r(in_array($obj, $result)); print_r("<P>");
if...
|
by: Craig Keightley |
last post by:
I am trying to compare values of a string entered into an array but having
no results, is this possible to achieve:
<?php
$ids = $row_rsProduct; // A comma separated list of values...
|
by: Tom Barnes |
last post by:
Check out this code:
// Start Code -------------
function test_in_array($val)
{
$a = array('key' => $val);
printf("in_array: %d, value:%s<BR>", in_array('key', $a), $a);
}
test_in_array(0);...
| |
by: rich |
last post by:
I keep getting the error:
Warning: in_array(): Wrong datatype for second argument on line 679
here is the code.
I declare and fill the array
$specprimA = array();
$specprimA=...
|
by: Sonnich |
last post by:
The following code is an exact copy of my current code.
The idea is to check 2 parts (of string arrays), and add those only
once to a common array.
I check for existance of an array in a string,...
|
by: Dhiru1009 |
last post by:
Hi guys,
I am trying to build a user registration form using PHP and MYSQL but encountring a problem.
When I click on submit with empty fields it adds records to database also it doesn't matter...
|
by: samz |
last post by:
Hello,
Here is a simple PHP recursive file list (with interactive and visual FX)
Sam's Files
http://acc.jexiste.ch/JPN/RecurciveDIR12.RAR
1. How to ignore/avoid empty folders in this PHP script...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |