Re: Speed optimisation
On 10/7/05, Alex Tweedly <[EMAIL PROTECTED]> wrote: So *if* Rob can accept the missing trailing "0" then Kay is the "winner > apparent" for the QP (pending any other faster method). Not so fast:-) Ithink Alex you were spot on when you said: 3. it's easy to get obsessive about speed beyond the point where it matters > - I do it all the time !! > Having been jolted out of 'reduce the speed' mode when my errors were pointed out it was time to see what other faults were out there. You've already mentioned lack of trailing zeros. I note also that only your code Alex includes a leading zero, ie 03:13:45.0, again, that may or may not be important. As far as my code not correctly showing hours greater than 24 it was most definitely a case of looking at the speed and not the output. In a button I put this code to feed the function, just commenting out the function I didn't want to run - then read the output from the message box. on mouseUp put the ticks into tStartTime repeat 15 times put return & addTime("18:26:43.5",random(2)) after tMyTime -- Wayne --put return & addDivandFormat("18:26:43.5",random(2)) after tMyTime --Alex --put return & addTimeAndSeconds("18:26:43.5",random(2)) after tMyTime-- Dick end repeat put (the ticks - tStartTime)/60 & tMyTime into msg end mouseUp I would have put a random time in but I just couldn't be bothered so I just made one up. It just happens that in this case there are only three instances where the result will exceed 23:59:59. The message box obviously contained the elapsed time at the top followed by 15 lines of results. I was too busy checking the time and a couple of pages of results to not notice that it never came up with an answer like 27:xx:xx.x I know some comment has gone back and forward between Dick and Alex about whether my code can handle negative seconds. Whilst I checked Dick's example and came to the same conclusion that Alex did - that my code doesn't have problems with negative seconds, the comments did make me realize that there was a SERIOUS error with my code. If someone takes the trouble of recording hh:mm:ss.s then there is a good chance that the seconds they want to add will also contain a decimal. In this case my code produces an error regardless of whether you add or subtract. I know Rob indicated that he was happly only to add time, which is lucky because this amended code will not work for subtract: function addTime pTime,pSeconds set the itemDelimiter to "." convert item 1 of pTime to seconds convert (item 1 of pTime + item 1 of pSeconds) to long time return it & "." & (item 2 of pTime + item 2 of pSeconds) end addTime But when I now run this new code it is about 0.1 seconds slower that Alex's code!! So not only does it appear that Alex's code is fastest, it works for both add and subract AND the format is consistent. I'll now put away my acceptance speech and return the tuxedo;-) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
Dick Kriesel wrote: On 10/6/05 4:21 AM, "Alex Tweedly" <[EMAIL PROTECTED]> wrote: Hmmm - can you give a case where Kay's method gives wrong answers because of a negative seconds value ? As far as I can see (both by inspection and by testing) it always gets it right. Whenever the negative seconds cause the result to be negative, the result is wrong. For example, "00:01:00.5",-61 gives 23:59:59.5. But that's the right answer ! 00:01:00.5 represents 60.5 seconds after midnight. Subtract 61 seconds and you should get .6 seconds before midnight - i.e. 23:59:59.5 But even if the convert function didn't cause a wrap-around, the result would be wrong, because the decimal digit is appended without checking the sign. The function would compute (60-61)&.5 = -1.5 seconds, instead of -.5 seconds. There's no need to check a sign here. Any fractional part of a second in the original time value is always positive; so you always want to add it. Since the result is also always positive (it uses wrap-around rather than return negative times), the positive fractional part should always be added. In the case you mention, (60-61)&.5 doesn't give -1.5, what it gives is the wrapped version, i.e. (something:59)&.5 On the other hand, both your method and mine (which is really just a variant of yours) get it wrong in key cases of negative seconds (e.g. 00:00:00.1 - 31 gives 0:00:0-31 for you and 0:00:0-31.0 for me - both rather hopeless :-) So I'm glad I wasn't throwing any stones. Here's a new version that handles negative seconds and negative results correctly (I think): No, it doesn't I'm afraid. 00:00:00.0 - 31 gives a wrong answer. You can fix that with an extra if test to put in a sign if the whole tSeconds is negative. But once you've done that, even using the "format" variant for its speed, it's slower than Kay's version using twelvehourtime. Rob indicated in another email that they could change the spec to allow wrap-around within the 24-hour space. So *if* Rob can accept the missing trailing "0" then Kay is the "winner apparent" for the QP (pending any other faster method). function addTimeAndSeconds pTime,pSeconds set the itemDelimiter to ":" put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime \ + pSeconds into tSeconds put tSeconds div 3600 & ":" & abs(tSeconds) mod 3600 div 60 & ":" \ & abs(tSeconds) mod 60 into tTime if item 2 of tTime < 10 then put 0 before item 2 of tTime if item 3 of tTime < 10 then put 0 before item 3 of tTime return tTime end addTimeAndSeconds The new version runs faster than my old, invalid version, oddly enough. I wonder if Rob still cares... -- Dick ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution -- Alex Tweedly http://www.tweedly.net No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.13/123 - Release Date: 06/10/2005 ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/6/05 4:21 AM, "Alex Tweedly" <[EMAIL PROTECTED]> wrote: > Hmmm - can you give a case where Kay's method gives wrong answers > because of a negative seconds value ? > As far as I can see (both by inspection and by testing) it always gets > it right. Whenever the negative seconds cause the result to be negative, the result is wrong. For example, "00:01:00.5",-61 gives 23:59:59.5. But even if the convert function didn't cause a wrap-around, the result would be wrong, because the decimal digit is appended without checking the sign. The function would compute (60-61)&.5 = -1.5 seconds, instead of -.5 seconds. > On the other hand, both your method and mine (which is really just a > variant of yours) get it wrong in key cases of negative seconds (e.g. > 00:00:00.1 - 31 gives 0:00:0-31 for you and 0:00:0-31.0 for me - > both rather hopeless :-) So I'm glad I wasn't throwing any stones. Here's a new version that handles negative seconds and negative results correctly (I think): function addTimeAndSeconds pTime,pSeconds set the itemDelimiter to ":" put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime \ + pSeconds into tSeconds put tSeconds div 3600 & ":" & abs(tSeconds) mod 3600 div 60 & ":" \ & abs(tSeconds) mod 60 into tTime if item 2 of tTime < 10 then put 0 before item 2 of tTime if item 3 of tTime < 10 then put 0 before item 3 of tTime return tTime end addTimeAndSeconds The new version runs faster than my old, invalid version, oddly enough. I wonder if Rob still cares... -- Dick ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/6/05, Alex Tweedly <[EMAIL PROTECTED]> wrote: > No, I don't think so, because your method gets wrong (IMHO) results in > two cases :-). > Well spotted. Yes I guess depending on what your going to do with it afterward it may matter. I tend not to bother with trailling zeros as Rev does a good job, put 20. + 20.5 will give you the correct answer. It's only if you have to display it does it matter, and in this case maybe the result is for final display. The other case I'm still looking into. I'm sure your numbers were the same as mine. More investigation requierd. And I was so looking forward to the QP Doll;-) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/6/05, Alex Tweedly <[EMAIL PROTECTED]> wrote: > No, I don't think so, because your method gets wrong (IMHO) results in > two cases :-). > Well spotted. Yes I guess depending on what your going to do with it afterward it may matter. I tend not to bother with trailling or leading zeros as Rev does a good job, put 20. + 20.5 + .25 will give you the correct answer. It's only if you have to display it does it matter, and in this case maybe the result is for final display. And I was so looking forward to the QP Doll;-) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
Dick Kriesel wrote: Not yet, Kay; that would be too easy. Although the "spec" didn't mention that the seconds might be negative, it didn't rule it out, either. The above function would return an incorrect value if there were a negative. And handling negatives correctly might increase the times and counts. Hmmm - can you give a case where Kay's method gives wrong answers because of a negative seconds value ? As far as I can see (both by inspection and by testing) it always gets it right. On the other hand, both your method and mine (which is really just a variant of yours) get it wrong in key cases of negative seconds (e.g. 00:00:00.1 - 31 gives 0:00:0-31 for you and 0:00:0-31.0 for me - both rather hopeless :-) Hey, Rob, since you wrote the "spec," do negatives matter? Yeah, yeah, it's the spec that's at fault, not the programmer :-) :-) -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.10/119 - Release Date: 04/10/2005 ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
Kay C Lan wrote: I certainly have to agee with Alex's statement about sitting back and watching. So here is my effort: ___ So do I win the Cupie doll? ;-) No, I don't think so, because your method gets wrong (IMHO) results in two cases :-). 1. 10:30:28 + 50 results in Alex 10:31:18.0 Kay10:31:18. (i.e. trailing full stop (period) rather than a "0" after it) 2. 23:59:59.3 + 50 results in Alex 24:00:49.4 Kay 00:00:49.4 and the original problem statement said "hh can be greater than 24!" which I take to mean that it should be in cases like this, to avoid problems of knowing whether or not the values have wrapped. Using the built in time functions will always wrap, so that may require the surrounding code to change. I've got the same amount of lines as Sarah's but more words; but less word's than Alex's, assuming you set the twelveHourTime in an openStack handler. for 15 calculations on my machine (run 3 consecutive times) IF the original time has no decimal ie 10:30:28 Dick 5.78, 5.83, 5.86 Alex 5.38, 5.73, 5.45 me 5.48, 5.46, 5.55 With decimal times ie 11:23:34.6 Dick 6.05, 6.21, 6.13 Alex 5.55, 5.65, 5.68 me 5.4, 5.46, 5.58 Dick's and Alex's seem to prefer no decimal, but mine doesn't seem fussed either way. Interesting. You didn't say what your machine was, but I get quite different results (Sony laptop, Pentium 4, WinXP), with my method consistently being faster then yours (note I set the twlevehourtime *outside* the timing loop). (You didn't say what number of seconds you were adding, so I couldn't reproduce the comparison exactly. Note it does make a difference to some methods whether or not you need to carry the increment - though probably not to either of these two methods) 12:11:12.2 + 50 Alex 2.636 2.687 2.682 Kay3.158 2.828 2.801 10:31:18 + 50 - Alex 2.332 2.307 2.227 Kay 2.888 2.879 2.888 11:23:34.6 + 50 --- Alex 2.415 2.438 2.471 Kay 2.707 2.704 2.731 I think all this proves is: 1. we're down to the point where differences are tiny 2. hardware and OS may give different results 3. it's easy to get obsessive about speed beyond the point where it matters - I do it all the time !! -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.10/119 - Release Date: 04/10/2005 ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/5/05 11:16 PM, "Kay C Lan" <[EMAIL PROTECTED]> wrote: > set the twelveHourTime to false --possibly set in openStack handler > > function addTime pTime,pSeconds > --set the twelveHourTime to false (if not set elsewhere in stack) > set the itemDelimiter to "." > convert item 1 of pTime to seconds > convert (item 1 of pTime + pSeconds) to long time > return it & "." & item 2 of pTime > end addTime > > So do I win the Cupie doll? ;-) Not yet, Kay; that would be too easy. Although the "spec" didn't mention that the seconds might be negative, it didn't rule it out, either. The above function would return an incorrect value if there were a negative. And handling negatives correctly might increase the times and counts. Hey, Rob, since you wrote the "spec," do negatives matter? I hadn't heard there was a Cupie doll available. Whether there is or not, maybe Rob will award the winner a virtual QP doll (for Quintessential Performance ;)) ). -- Dick ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/5/05, Alex Tweedly <[EMAIL PROTECTED]> wrote: Amazing how much it helps to be slow in replying, you get to use the > best ideas from all previous entries :-) > > It's one less statement than Sarah's (though more characters) so it > competes for shortest. > > On my machine (for 150,000), > Dick's method took 3.76 seconds > Alex's method took 2.43 seconds > > > function addDivandFormat pTime,pSeconds > set the itemDelimiter to ":" > put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime + > pSeconds into tSeconds > return format("%02d:%02d:%04.1f", tSeconds div 3600, tSeconds mod 3600 > div 60, tSeconds mod 60) > end addDivandFormat I certainly have to agee with Alex's statement about sitting back and watching. So here is my effort: I've got the same amount of lines as Sarah's but more words; but less word's than Alex's, assuming you set the twelveHourTime in an openStack handler. --- set the twelveHourTime to false --possibly set in openStack handler function addTime pTime,pSeconds --set the twelveHourTime to false (if not set elsewhere in stack) set the itemDelimiter to "." convert item 1 of pTime to seconds convert (item 1 of pTime + pSeconds) to long time return it & "." & item 2 of pTime end addTime for 15 calculations on my machine (run 3 consecutive times) IF the original time has no decimal ie 10:30:28 Dick 5.78, 5.83, 5.86 Alex 5.38, 5.73, 5.45 me 5.48, 5.46, 5.55 With decimal times ie 11:23:34.6 Dick 6.05, 6.21, 6.13 Alex 5.55, 5.65, 5.68 me 5.4, 5.46, 5.58 Dick's and Alex's seem to prefer no decimal, but mine doesn't seem fussed either way. So do I win the Cupie doll? ;-) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
Sarah Reichelt wrote: I tested the suggestions and a couple of my own and couldn't find anything faster than Wouter's suggestions, with Dick's being very similar. I challenge for the shortest function :-) function newTime pTime, pAdd set the itemDel to ":" add pAdd to item 3 of pTime convert pTime to long time return char -8 to -1 of ("0" & pTime) end newTime But it takes about the same time as the others. However re-cheking Rob's original specs, I see he uses seconds with 1 decimal place, and my function won't handle that :-( Wouter's won't either, so Dick's looks like the best bet at this stage. I challenge for shortest and fastest. (Amazing how much it helps to be slow in replying, you get to use the best ideas from all previous entries :-) It's one less statement than Sarah's (though more characters) so it competes for shortest. On my machine (for 150,000), Dick's method took 3.76 seconds Alex's method took 2.43 seconds function addDivandFormat pTime,pSeconds set the itemDelimiter to ":" put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime + pSeconds into tSeconds return format("%02d:%02d:%04.1f", tSeconds div 3600, tSeconds mod 3600 div 60, tSeconds mod 60) end addDivandFormat -- Alex Tweedly http://www.tweedly.net -- Internal Virus Database is out-of-date. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.8/114 - Release Date: 28/09/2005 ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
I tested the suggestions and a couple of my own and couldn't find anything faster than Wouter's suggestions, with Dick's being very similar. I challenge for the shortest function :-) function newTime pTime, pAdd set the itemDel to ":" add pAdd to item 3 of pTime convert pTime to long time return char -8 to -1 of ("0" & pTime) end newTime But it takes about the same time as the others. However re-cheking Rob's original specs, I see he uses seconds with 1 decimal place, and my function won't handle that :-( Wouter's won't either, so Dick's looks like the best bet at this stage. Sorry, Sarah ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
On 10/4/05 11:58 PM, "Rob Beynon" <[EMAIL PROTECTED]> wrote: > Dear Colleagues, > > I have a function that takes a time in this format > > hh:mm:ss.s > > and to which I add a variable number of seconds, then output the > updated time in the same format. hh can be greater than 24! > > Here's the function. Problem is, it seems slow (I need to do this call > about 150,000 times each file I process). I would appreciate any > insights into making this function faster > > function newTime oldTime,addedSec > put the replacetext(oldTime,":"," ") into splitTime > put the first word of splitTime into h > put the second word of splitTime into m > put the third word of splitTime into s > put s + addedSec into newSec > put newSec mod 60 into remainSec > put (newsec-remainSec)/60 into addedMin > put m + addedMin into newMin > put newMin mod 60 into remainMin > put (newMin-remainMin)/60 into addedHr > put h + addedHr into newHr > if length(remainSec) = 1 then put "0" & remainSec into remainSec > if length(remainMin) = 1 then put "0" & remainMin into remainMin > if length(newHr) = 1 then put "0" & newHr into newHr > put newHr & ":" & remainMin & ":" & remainSec into newTime > return newTime > end newTime On my machine, executing your function 150,000 times took 15 seconds. Unless you have a lot of files or you're really pressed for time, your function seems fast enough to me. But if you feel the need for speed, here's a similar function, that worked in my few tests, and that took 5.7 seconds for 150,000 executions: function addTimeAndSeconds pTime,pSeconds set the itemDelimiter to ":" put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime \ + pSeconds into tSeconds put tSeconds div 3600 & ":" & tSeconds mod 3600 div 60 & ":" \ & tSeconds mod 60 into tTime if item 2 of tTime < 10 then put 0 before item 2 of tTime if item 3 of tTime < 10 then put 0 before item 3 of tTime return tTime end addTimeAndSeconds I'd expect there'd be an even faster way. (Sarah?) -- Dick ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
I wrote the following alternative. It's about twice as fast, which is less than I had hoped for. One real speed gain if your data is at all repetitive would be to memo-ize your function. Keep a table of a few thousand inputs and outputs. Whenever you get a call with the same values that you've done before, you'll have the answer as quickly as an array lookup. Obviously if your data isn't repetitive this doesn't help. It seems like a good guess when calling a routine 150,000 times that you'll be calling it with the same arguments a few times. regards, geoff function newTime T,pSeconds split T using ":" add pSeconds to T[3] repeat with i = 3 to 2 step -1 get T[i] div 60 subtract 60 * it from T[i] add it to T[(i-1)] end repeat repeat with i = 1 to 3 if T[i] < 10 then put "0" before T[i] end repeat return T[1] & ":" & T[2] & ":" & T[3] end newTime On Oct 4, 2005, at 11:58 PM, Rob Beynon wrote: Dear Colleagues, I have a function that takes a time in this format hh:mm:ss.s and to which I add a variable number of seconds, then output the updated time in the same format. hh can be greater than 24! Here's the function. Problem is, it seems slow (I need to do this call about 150,000 times each file I process). I would appreciate any insights into making this function faster function newTime oldTime,addedSec put the replacetext(oldTime,":"," ") into splitTime put the first word of splitTime into h put the second word of splitTime into m put the third word of splitTime into s put s + addedSec into newSec put newSec mod 60 into remainSec put (newsec-remainSec)/60 into addedMin put m + addedMin into newMin put newMin mod 60 into remainMin put (newMin-remainMin)/60 into addedHr put h + addedHr into newHr if length(remainSec) = 1 then put "0" & remainSec into remainSec if length(remainMin) = 1 then put "0" & remainMin into remainMin if length(newHr) = 1 then put "0" & newHr into newHr put newHr & ":" & remainMin & ":" & remainSec into newTime return newTime end newTime -- All best wishes, Rob (Created at 07:55 on 05/10/2005) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution
Re: Speed optimisation
Hi, May be this will do? (just to give an idea, can be tweaked) function newTime oldTime,addedSec set the twelvehourtime to false convert oldTime to seconds add addedSec to oldTime convert oldTime to long time if length(oldTime) = 8 then return oldTime else return "0" & oldTime end newTime Greetings, Wouter On 05 Oct 2005, at 08:58, Rob Beynon wrote: Dear Colleagues, I have a function that takes a time in this format hh:mm:ss.s and to which I add a variable number of seconds, then output the updated time in the same format. hh can be greater than 24! Here's the function. Problem is, it seems slow (I need to do this call about 150,000 times each file I process). I would appreciate any insights into making this function faster function newTime oldTime,addedSec put the replacetext(oldTime,":"," ") into splitTime put the first word of splitTime into h put the second word of splitTime into m put the third word of splitTime into s put s + addedSec into newSec put newSec mod 60 into remainSec put (newsec-remainSec)/60 into addedMin put m + addedMin into newMin put newMin mod 60 into remainMin put (newMin-remainMin)/60 into addedHr put h + addedHr into newHr if length(remainSec) = 1 then put "0" & remainSec into remainSec if length(remainMin) = 1 then put "0" & remainMin into remainMin if length(newHr) = 1 then put "0" & newHr into newHr put newHr & ":" & remainMin & ":" & remainSec into newTime return newTime end newTime -- All best wishes, Rob (Created at 07:55 on 05/10/2005) ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution ___ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution