2007. 03. 22, csütörtök keltezéssel 12.58-kor tedd ezt írta: > At 4:53 PM +0100 3/22/07, Németh Zoltán wrote: > >2007. 03. 22, csütörtök keltezéssel 11.42-kor tedd ezt írta: > > > > > As for efficiency, that's probably not even worth mentioning in this case. > > > >why not? you would use 2 sql queries while I would use one most of the > >time and 2 in case of already reserved ID - that is almost twice as much > >processor time > >that may count much if the routine is used frequently > > Go ahead, try it, and tell me how much time it cost. > > From experience, there are things one can spend > their time optimizing and there are other things > that one should be able to see that aren't worth > pursing. > > I've been wrong before, but you're open to prove me wrong again, if you like. okay, I've got up earlier and made a little benchmark. The result suprised me, and proved you right. ;) here is the code: <?php $db = mysql_connect("localhost", "testuser", "test"); mysql_select_db("testdb"); // method 1 $time = microtime(TRUE); for ($i = 1; $i <= 5000; $i++) { $done = FALSE; while (!$done) { $id = md5((microtime(TRUE) * (rand(1,1000) / 100))); $sql = "SELECT * FROM idtest WHERE id='$id'"; $result = mysql_query($sql); if (!($row = mysql_fetch_array($result))) { $done = TRUE; } } $sql = "INSERT INTO idtest (id,cnt) VALUES ('$id', $i)"; mysql_query($sql); } $end = microtime(TRUE) - $time; echo "method 1 time: " . $end . "<br>"; // clean up table $sql = "DELETE FROM idtest"; mysql_query($sql); // method 2 $time = microtime(TRUE); for ($i = 1; $i <= 5000; $i++) { $done = FALSE; while (!$done) { $id = md5((microtime(TRUE) * (rand(1,1000) / 100))); $sql = "INSERT INTO idtest (id,cnt) VALUES ('$id', $i)"; $result = mysql_query($sql); if ($result) { $done = TRUE; } } } $end = microtime(TRUE) - $time; echo "method 2 time: " . $end . "<br>"; ?> and the output: method 1 time: 97.1539468765 method 2 time: 93.4973130226 this clearly shows that the method I suggested is only slightly faster, so that difference really isn't worth mentioning as you said :) however, the difference may be slightly bigger when using better ID generation functions, mine above is very basic so it might have more repetitions... greets Zoltán Németh > > Cheers, > > tedd > -- > ------- > http://sperling.com http://ancientstones.com http://earthstones.com > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php