Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <2051024.CPFVgIA8O1@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 16 Feb 2014 22:16:29 +0100 User-Agent: KNode/4.11.5 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <1816957.0LDT0NebiR@PointedEars.de> <0.9da5fd4e321b89e9adc3.20140216202439GMT.877g8u7qm0.fsf@bsb.me.uk> <0.1d3b7c8128d5ae4333af.20140216205119GMT.87siri6at4.fsf@bsb.me.uk> MIME-Version: 1.0 Lines: 55 NNTP-Posting-Date: 16 Feb 2014 22:16:31 CET NNTP-Posting-Host: 6b3b35d6.newsspool1.arcor-online.net X-Trace: DXC=Ddf:6]=Il1ffF8a^:6>b7eic==]BZ:afn4Fo<]lROoRankgeX?EC@@`JMNQ;63YamdDZm8W4\YJNlYUVHKH;GI^gnQi[[ZMHf2`QM3iaPlo;dk X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.php:12852 Ben Bacarisse wrote: > "J.O. Aho" writes: >> On 16/02/14 21:24, Ben Bacarisse wrote: >>> Thomas 'PointedEars' Lahn writes: >>>> Jason C wrote: >>> >>>>> I'm looping through roughly 100,000 values: >>>>> >>>>> $userArr = array( >>>>> "something" => "anything", >>>>> ... >>>>> ); >>>>> >>>>> foreach ($userArr as $key => $val) { >>>>> $firstname = str_replace($key, "****", $firstname); >>>>> $lastname = str_replace($key, "****", $lastname); >>>>> } >>>>> >>>>> Both $firstname AND $lastname could contain any of the keys, but could >>>>> only potentially contain one key each. It's likely, though, that >>>>> neither will contain any of the keys. >>> >>>>> Would it be faster to use strpos() before each str_replace(), like >>>>> so?: >>>>> >>>>> foreach ($userArr as $key => $val) { >>>>> if (strpos($firstname, $key) !== false) >>>>> $firstname = str_replace($key, "****", $firstname); >>>>> >>>>> if (strpos($lastname, $key) !== false) >>>>> $lastname = str_replace($key, "****", $lastname); >>>>> } >>>> >>>> Most certainly not. >>> >>> And yet, when I measured it, it was faster. How can that be? >>> >>> >> >> For strpos is faster than str_replace on finding the string. How, and why? Both search for the occurrence of a substring in a string. > Yes, though the question was rhetorical! The question was stupid. PointeDears -- Sometimes, what you learn is wrong. If those wrong ideas are close to the root of the knowledge tree you build on a particular subject, pruning the bad branches can sometimes cause the whole tree to collapse. -- Mike Duffy in cljs,