>>at first, both sample has same loop - one No they do NOT have the same loop. Yes they each have one loop, but in the first case you are iterating through the loop once for every checkbox in the system, and you are retrieving the ID and doing a regex compare, and then maybe doing a set. If there are 111 checkboxes, you do the loop 111 times
In the second you are pre-filtering as it were, and passing to the loop ONLY the checkboxes that have ID's that match. So if there are only 11 checkboxes that match the ID pattern, it only does the loop 11 times. Furthermore what's inside the loop is simpler, it's just setting the checkbox If you don't believe me, create an iterator for each loop and have it do a puts of the iteration number every time it goes through the loop. In your 'test' code you have no 'control' to compare against, and things are bound to be different since there is no interacting with the DOM or the browser. So instead you should compare what you did, with a pre-filtered loop that does basically the same thing (increments cc.. the only 'work' being done by the loop) If we let cc+=1 stand in for the operation being done by the loop, then to get a true comparison, compare the 20 seconds to do the loop you had in your test code, to the time to do something like this src = array of 111 elements def Z1(src) cc=0 src.each do |l| cc+=1 end cc end Neither one includes the time it takes to 'create' the array that is being processed, which is another aspect of the time involved, however given the 3 second time of the second approach, I think we can safely assume that the .elements method is able to build it's list of matching elements fairly rapidly. OTOH the other loop, the list of checkbox objects may not contain the ID information. I don't have enough info to know what information is actually included inside the collection returned by .checkboxes, to know if this is the case or not, but potentially c.id is acting like a method (as opposed to a property) and having to go to the dom and get that info each time, which could be adding time, and then the system has to do the regex compare as well. On Apr 25, 4:05 am, Sergey Erokhin <sergey.erok...@gmail.com> wrote: > sorry joedio, but you are wrong ;) > > at first, both sample has same loop - one > and faster example has much more regexp (each checkox placed in > different table cell, and a lot of different elements on page) > > and if in slow code add printout (like puts c.id, each next puts get > more time, looks like each next took time of all previously and self > time) > > and especially for you > > src = array of 500 elements where 111 match regexp > > def Z1(src) > cc=0 > src.each do |l| > next unless l =~ /ctl00_cph_rpt.*?Notif.*?_eml_ctl\d+_chkEmail/ > cc+=1 > end > cc > end > Benchmark.bm(7) do |x| > x.report("z1:") { (1..10000).each { Z1($src) }} > end > user system total real > z1: 20.500000 0.010000 20.510000 ( 20.515719) > > execution time about 20 second, there a MUCH more regex and loops than > in my first code ;) > > > > > > > > > I wouldn't say its a bug, but that its a fine example of how > > different coding techniques can affect performance. > > Thus it makes sense that the second example will run faster: > > Less loops means faster execution > > Fewer RegExp checks also means faster execution -- Before posting, please read http://watir.com/support. In short: search before you ask, be nice. watir-general@googlegroups.com http://groups.google.com/group/watir-general watir-general+unsubscr...@googlegroups.com