in_array performance in unsorted vs sorted array [message #178204] |
Wed, 23 May 2012 06:20  |
William Gill
Messages: 31 Registered: March 2011
Karma: 0
|
Member |
|
|
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?
|
|
|
|
|
|
|
Re: in_array performance in unsorted vs sorted array [message #178248 is a reply to message #178205] |
Thu, 24 May 2012 20:20  |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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)
|
|
|