Jump to content

Recommended Posts

Posted

Hello, I am trying to make a simple script to display things that happen inside of a loop like time remaining, percent done, and so on... But it is turning out to be very confusing. I know the code should work but am tired of looking at it so can someone with a fresh eye analyze it for me? :( Thanks in advance. :(

Here's the code:

$max = 1500
$start = TimerInit()

For $i = 0 to $max
    $percent_done = ( ( $i / $max ) * 100 )
    $percent_left = ( 100 - $percent_done )
    $time_elapsed = ( TimerDiff($start) / 1000 )
    $time_per_percent = ( $percent_done / $time_elapsed )
    $time_total = ( $time_per_percent * 100 )
    $time_remaining = ( $time_total - $time_elapsed )
    ToolTip("Seconds left: " & $time_remaining & @CRLF & "Percent done: " & $percent_done & @CRLF & "Time elapsed: " & $time_elapsed & @CRLF & "Total time: " & $time_total)
    Sleep(1)
Next

MsgBox(0, "", "")
Posted (edited)

What is $time_remaining supposed to show ? :(

Time remaining of what ? Of the loop ?

Also, you can't mean that $time_total is supposed to show time spent

in the loop, because that's $time_elapsed's job.. :(

None of them are very useful or even logical.

Please explain.

(Btw, I've also been up for a while plus that my math sucks. Big time. Seriously.)

(Edit : Made the sentence above a little bit clearer..)

Edited by Helge
Posted

What is $time_remaining supposed to show ?  :(

Time remaining of what ? Of the loop ?

<{POST_SNAPBACK}>

Yes, of the loop. I just need it to display the time remaining, etc... of the loop for whatever value $max is.
Posted

$max = 111
$time_elapsed = 0
For $i = 0 to $max
    $start = TimerInit()
   ;
   ;do the real work of the loop here
    Sleep(1000)
   ;if the time per loop varies greatly...
   ;then code below is of little value
   ;
    $percent_done = (($i/$max) * 100)
    $percent_left = (100 - $percent_done)
    $time_per_loop = (TimerDiff($start) / 1000)
    $time_elapsed = $time_elapsed + $time_per_loop
    $time_total = ($time_per_loop * $max)
    $time_remaining = ($time_total - $time_elapsed)
    ToolTip("Seconds left: " & $time_remaining & @CRLF &_
               "Percent done: " & $percent_done & @CRLF &_
               "Time elapsed: " & $time_elapsed & @CRLF &_
               "Total time: " & $time_total)
Next

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted (edited)

How can AutoIt possibly tell how long a loop is going to execute?

<{POST_SNAPBACK}>

By telling how much time it takes to execute 1 percent of the loop and multiplying that by 100. It is just guesstimating on that just how windows does for the time remaining on your download bar.

Also, thanks for your help herewasplato but your program was kind of glitchy since it is based of CPU speed. I saw the time remaining go into negative decimals a couple of times. Thanks for your try anyway but since windows does it why can't AutoIt?

Next, i have an idea but i'm not sure how to implement it. To make the time remaining more accurate, as it goes forward in the loop it will take the time per 1 percent it has done and multiply that by 100 since 100 / 1 = 100. The next time it goes through the loop, since it has 2 percent done it will take the time it took to get 2 percent done and multiply that by 50 since 100 / 2 = 50. Next time though it will get 3 percent done and multiply that by 33.33 because 100 / 3 = 33.33... and so on. Any ideas on how to actually do this?

Edited by datkewlguy
Posted

Also, thanks for your help herewasplato but your program was kind of glitchy since it is based of CPU speed.

<{POST_SNAPBACK}>

Yes, I know - hence my caveat about loop time variations...

Just add that one second delay and it will be more stable :-)

Or use your own code - but change:

$time_per_percent = ( $percent_done / $time_elapsed )

to

$time_per_percent = ( $time_elapsed / $percent_done)

I could not find a flaw in your logic/code this morning - hence the horrible work around code that I posted.

Nice code - consider it copied.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted (edited)

I'm reviving this topic lol. I want to help conquer this. It could then be made into a function like...

_LoopTime($nVar)

Eg usage.

$max = 111
_LoopTime($max)
For $i = 0 to $max
;blah
Next

And it could return an array of the time left, percent blah blah etc.

Maybe I'm just dreaming lol, but anyway's, if anyone is still interested I would like to help.

Edited by Burrup

qq

Posted

Since I have recently developed an interest in Timer functions then I think I will give this a shot :(.

Give me a bit to work up some code based on the above examples as well as some ideas of my own and that were submitted.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Posted

Since I have recently developed an interest in Timer functions then I think I will give this a shot :(.

Give me a bit to work up some code based on the above examples as well as some ideas of my own and that were submitted.

JS

<{POST_SNAPBACK}>

thankyou! so far all our info is based off of computer speed and we cant incorporate this into programs that need to run quickly.. Your help is very much appreciated!
Posted (edited)

thankyou! so far all our info is based off of computer speed and we cant incorporate this into programs that need to run quickly.. Your help is very much appreciated!

<{POST_SNAPBACK}>

What is wrong with your original code with the correction that I posted on 30 May?
$max = 1500
$start = TimerInit()

For $i = 0 to $max
    $percent_done = ( ( $i / $max ) * 100 )
    $percent_left = ( 100 - $percent_done )
    $time_elapsed = ( TimerDiff($start) / 1000 )

    $time_per_percent = ( $time_elapsed / $percent_done)
   ;$time_per_percent = ( $percent_done / $time_elapsed )

    $time_total = ( $time_per_percent * 100 )
    $time_remaining = ( $time_total - $time_elapsed )
    ToolTip("Seconds left: " & $time_remaining & @CRLF &_
               "Percent done: " & $percent_done & @CRLF &_
               "Time elapsed: " & $time_elapsed & @CRLF &_
               "Total time: " & $time_total)
    Sleep(10)
Next

MsgBox(0, "", "")
Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted (edited)

What is wrong with your original code with the correction that I posted on 30 May?

$max = 1500
$start = TimerInit()

For $i = 0 to $max
    $percent_done = ( ( $i / $max ) * 100 )
    $percent_left = ( 100 - $percent_done )
    $time_elapsed = ( TimerDiff($start) / 1000 )

    $time_per_percent = ( $time_elapsed / $percent_done)
 ;$time_per_percent = ( $percent_done / $time_elapsed )

    $time_total = ( $time_per_percent * 100 )
    $time_remaining = ( $time_total - $time_elapsed )
    ToolTip("Seconds left: " & $time_remaining & @CRLF &_
               "Percent done: " & $percent_done & @CRLF &_
               "Time elapsed: " & $time_elapsed & @CRLF &_
               "Total time: " & $time_total)
    Sleep(10)
Next

MsgBox(0, "", "")

<{POST_SNAPBACK}>

Edit: Here is the code to round it. It still is CPU dependant, but I dont think there is really a way to get around that. Gonna see what else I can come up with.

$max = 1500
$start = TimerInit()

For $i = 0 to $max
    $percent_done = Round(( ( $i / $max ) * 100 ))
    $percent_left = ( 100 - $percent_done )
    $time_elapsed = Round(( TimerDiff($start) / 1000 ))

    $time_per_percent = ( $time_elapsed / $percent_done )
  ;$time_per_percent = ( $percent_done / $time_elapsed )

    $time_total = Round(( $time_per_percent * 100 ))
    $time_remaining = Round(( $time_total - $time_elapsed ))
    ToolTip("Seconds left: " & $time_remaining & @CRLF &_
               "Percent done: " & $percent_done & @CRLF &_
               "Time elapsed: " & $time_elapsed & @CRLF &_
               "Total time: " & $time_total)
    Sleep(10)
Next

MsgBox(0, "", "")

Nothing. I never looked at your code throughly and I just compared it to mine and it works great. The only thing I have been playing with now is to round the numbers. I use the round and it just makes some of the numbers negative and messes everything up :(

JS

Edited by JSThePatriot

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Posted

@JSThePatriot

I would suggest that you not round while you still need the precision to do the math.

Try this:

ToolTip("Seconds left: " & StringFormat("%.1f", $time_remaining) & @CRLF &_
        "Percent done: " & StringFormat("%.1f", $percent_done) & @CRLF &_
        "Time elapsed: " & StringFormat("%.1f", $time_elapsed) & @CRLF &_
        "Total time: " & StringFormat("%.1f", $time_total))
If the time per loop varied widly, you could add some "output smoothing" by only updating the output if certain conditions were met... but this code works well (as is) for the loops that I have in mind...

@datkewlguy

You said, "...but since windows does it..." Can you give me an example of when/where Windows "does it". I can only think of the time estimates on file transactions like copy/moves - and they are poor estimates.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted

Instead of rounders I use the below to truncate everything after the decimal place including the dp itself. Only problem is that $time_total is always be $time_elapsed when its finished. $time_total doesn't change, so if anyone knows why :(.

$max = 1500
$start = TimerInit()

For $i = 0 to $max

    $percent_done = ( $i / $max ) * 100
    $percent_left = 100 - $percent_done

    $time_elapsed = TimerDiff($start) / 1000
    $time_per_percent = $time_elapsed / $percent_done

    $time_total = $time_per_percent * 100
    $time_remaining = $time_total - $time_elapsed

    ToolTip("Seconds left: " & _Truncate($time_remaining) & @CRLF &_
        "Percent done: " & _Truncate($percent_done) & @CRLF &_
        "Time elapsed: " & _Truncate($time_elapsed) & @CRLF &_
        "Total time: " & _Truncate($time_total))
    Sleep(10)
Next

MsgBox(0, "", "")

Func _Truncate($iVal)
   Return StringLeft($iVal,StringInStr($iVal,".") - 1)
EndFunc

qq

Posted (edited)

Time total isnt the one that is supposed to change. It is supposed to be constant so it gives the time remaining based on the time spent. It will calculate how long each loop takes and then return a value on that. Might be a away to even get it to average the value of the loops... Dunno if that would be feasable, or worth it but maybe worth a try.

Edit: In the edited example I gave above rounding the way I did it works fine. Everything ends up being the right numbers all the way through. Now with data in there or something else to increase the time it would have to be tested further, but I am working on it.

JS

Edited by JSThePatriot

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Posted (edited)

I used my way because with rounding the numbers it will jump very quickly like 15 > 16 > 15 > 14. When you first start it, with rounding, you can see it clearly that the numbers don't decrease in a steady decline, they jump as said above. By truncating you can eliminate this problem, nearly.There will still be a slight jump because of the decimal places but this is the best you can get with rounding the timer / 1000 part first.

Edited by Burrup

qq

Posted

minor comments:

We should change the test code to sleep at the start of the loop. The way we had it, the first $time_elapsed value was extremely small no matter what we changed the sleep length to...

I also changed

For $i = 0 to $max

For $i = 1 to $max

If we use $i from the "for/next" code within our time calculations, then it should never be 0. If I understand the code correctly, all of the time calculations would be completed at the end of each loop. $percent_done for the first time thru the loop should be 1/$max and not 0/$max. We might want to use a different counter to let people use 0 in a "for/next" statement.

$max = 1500
$start = TimerInit()

For $i = 1 to $max
   ;code to be timed goes here
    Sleep(100);simulate code to be timed
   ;Sleep(Random(1,100,1));simulate variations in code to be timed
    
    $percent_done = ( ( $i / $max ) * 100 )
    $percent_left = ( 100 - $percent_done )
    
    $time_elapsed = ( TimerDiff($start) / 1000 )
    $time_per_percent = ( $time_elapsed / $percent_done)
    
    $time_total = ( $time_per_percent * 100 )
    $time_remaining = ( $time_total - $time_elapsed )
    
    ToolTip("Seconds left: " & StringFormat("%.0f", $time_remaining) & @CRLF &_
               "Percent done: " & StringFormat("%.0f", $percent_done) & @CRLF &_
               "Time elapsed: " & StringFormat("%.0f", $time_elapsed) & @CRLF &_
               "Total time: " & StringFormat("%.0f", $time_total))
Next

MsgBox(0, "", "")

@Burrup - is there an advantage to StringLeft vs StringFormat that I'm missing here. Just asking to learn.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Posted (edited)

Well I know very little about StringFormat() but I'm learning. From the help file I gathered...

The width specification never causes a value to be truncated.

Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.

f = The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

Taken from different parts of StringFormat().

So StringFormat("%.1f", $Blah) or StringFormat("%.0f", $Blah), I think, is still rounding the value? I think thats right. All I know is that I used StringLeft because, as you can see I'm not entirely capable or comfortable with using StringFormat() atm :( and I know exactly what StringLeft() does.

Edit: Just done some testing. StringFormat() is faster then StringLeft() by a couple seconds.

Edited by Burrup

qq

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...