Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6124 > unrolled thread

in_array performance in unsorted vs sorted array

Started byWilliam Gill <noreply@domain.invalid>
First post2012-05-23 02:20 -0400
Last post2012-05-23 02:36 -0700
Articles 6 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  in_array performance in unsorted vs sorted array William Gill <noreply@domain.invalid> - 2012-05-23 02:20 -0400
    Re: in_array performance in unsorted vs sorted array Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-23 08:16 -0400
      Re: in_array performance in unsorted vs sorted array William Gill <noreply@domain.invalid> - 2012-05-23 10:57 -0400
        Re: in_array performance in unsorted vs sorted array William Gill <noreply@domain.invalid> - 2012-05-23 11:01 -0400
      Re: in_array performance in unsorted vs sorted array Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-05-24 22:20 +0200
    Re: in_array performance in unsorted vs sorted array Captain Paralytic <paul_lautman@yahoo.com> - 2012-05-23 02:36 -0700

#6124 — in_array performance in unsorted vs sorted array

FromWilliam Gill <noreply@domain.invalid>
Date2012-05-23 02:20 -0400
Subjectin_array performance in unsorted vs sorted array
Message-ID<jphvj0$acu$1@dont-email.me>
I am reading transaction records from files.  Each record has an 
alphanumeric GUID but that record may be repeated in more than one file 
(because of overlapping samples).  I don't want to process duplicate 
records, so I am considering a simple flat file to store the GUID's of 
previously processed records.

To keep things simple I plan to use $done=file() to read the flat file, 
and a simple if in_array to see if the current GUID has already been 
processed, if not process the current record and add its GUID to $done.

Does anyone know if sorting an array has any significant impact on 
in_array, or can I simply push push values into $done?

Also is there a better way than foreach() write() to get $done back into 
the flat file?

[toc] | [next] | [standalone]


#6125

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-23 08:16 -0400
Message-ID<jpikfl$kae$1@dont-email.me>
In reply to#6124
On 5/23/2012 2:20 AM, William Gill wrote:
> I am reading transaction records from files. Each record has an
> alphanumeric GUID but that record may be repeated in more than one file
> (because of overlapping samples). I don't want to process duplicate
> records, so I am considering a simple flat file to store the GUID's of
> previously processed records.
>
> To keep things simple I plan to use $done=file() to read the flat file,
> and a simple if in_array to see if the current GUID has already been
> processed, if not process the current record and add its GUID to $done.
>
> Does anyone know if sorting an array has any significant impact on
> in_array, or can I simply push push values into $done?
>
> Also is there a better way than foreach() write() to get $done back into
> the flat file?
>

Arrays in PHP are associative; their keys are handled as hash values. 
So I suspect it makes no difference on whether the array is sorted or not.

Also, what's wrong with foreach() write()?  That's how you get arrays 
back into a file.  Ensure you lock the file so that you don't have two 
scripts running against it at the same time.

But it sounds like you should be using a database.  That would solve a 
lot of your problems.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6131

FromWilliam Gill <noreply@domain.invalid>
Date2012-05-23 10:57 -0400
Message-ID<jpits7$dq8$1@dont-email.me>
In reply to#6125
On 5/23/2012 8:16 AM, Jerry Stuckle wrote:
>
> Arrays in PHP are associative; their keys are handled as hash values. So
> I suspect it makes no difference on whether the array is sorted or not.
>
OK, That seems to be in sync with what I'm (not) finding in Google.

> Also, what's wrong with foreach() write()? That's how you get arrays
> back into a file. Ensure you lock the file so that you don't have two
> scripts running against it at the same time.
>
Nothing.  I just wanted to be sure I wasn't unaware of something better, 
like a function inverse to file().  Probably looking at less then 100k 
records at any time so I guess performance won't be a problem.

> But it sounds like you should be using a database. That would solve a
> lot of your problems.
>
Yes it does, and that's where this is heading.  Right now it is just 
summarizing some information, but eventually this will become a 
pre-processor for a db.

[toc] | [prev] | [next] | [standalone]


#6132

FromWilliam Gill <noreply@domain.invalid>
Date2012-05-23 11:01 -0400
Message-ID<jpiu3k$g7p$1@dont-email.me>
In reply to#6131
On 5/23/2012 10:57 AM, William Gill wrote:
> On 5/23/2012 8:16 AM, Jerry Stuckle wrote:
>>
>> Arrays in PHP are associative; their keys are handled as hash values. So
>> I suspect it makes no difference on whether the array is sorted or not.
>>
> OK, That seems to be in sync with what I'm (not) finding in Google.
>
>> Also, what's wrong with foreach() write()? That's how you get arrays
>> back into a file. Ensure you lock the file so that you don't have two
>> scripts running against it at the same time.
>>
> Nothing. I just wanted to be sure I wasn't unaware of something better,
> like a function inverse to file(). Probably looking at less then 100k
> records at any time so I guess performance won't be a problem.
>
>> But it sounds like you should be using a database. That would solve a
>> lot of your problems.
>>
> Yes it does, and that's where this is heading. Right now it is just
> summarizing some information, but eventually this will become a
> pre-processor for a db.

Pulled the trigger too soon.  forgot to say Thanks.

[toc] | [prev] | [next] | [standalone]


#6168

FromThomas Mlynarczyk <thomas@mlynarczyk-webdesign.de>
Date2012-05-24 22:20 +0200
Message-ID<jpm57j$lkm$1@news.albasani.net>
In reply to#6125
Jerry Stuckle schrieb:

>> [Performance of in_array()]
> Arrays in PHP are associative; their keys are handled as hash values. So 
> I suspect it makes no difference on whether the array is sorted or not.

The /keys/ are hash values, yes. But in_array() searches through the 
/values/, not the keys, so I suppose (haven't tested it) the performance 
is O(n) rather than O(1). On the other hand, I doubt if PHP keeps track 
of whether the array is sorted or not, so it probably makes no 
difference indeed, as you said.

Greetings,
Thomas

-- 
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)

[toc] | [prev] | [next] | [standalone]


#6157

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-05-23 02:36 -0700
Message-ID<d56a223d-1d1b-4b84-9adb-3bba2c2ad065@w13g2000vbc.googlegroups.com>
In reply to#6124
On May 23, 7:20 am, William Gill <nore...@domain.invalid> wrote:
> I am reading transaction records from files.  Each record has an
> alphanumeric GUID but that record may be repeated in more than one file
> (because of overlapping samples).  I don't want to process duplicate
> records, so I am considering a simple flat file to store the GUID's of
> previously processed records.
>
> To keep things simple I plan to use $done=file() to read the flat file,
> and a simple if in_array to see if the current GUID has already been
> processed, if not process the current record and add its GUID to $done.
>
> Does anyone know if sorting an array has any significant impact on
> in_array, or can I simply push push values into $done?
>
> Also is there a better way than foreach() write() to get $done back into
> the flat file?

Why not load the files into a database table which has a primary key
of the GUID, then you have one record for each GUID

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web