Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-17-2020, 02:54 AM
alex100 alex100 is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows 7 64bit Short loop takes no less than 12s to run, uses 25% CPU power! Office 2016
Advanced Beginner
Short loop takes no less than 12s to run, uses 25% CPU power!
 
Join Date: May 2020
Posts: 56
alex100 is on a distinguished road
Default Short loop takes no less than 12s to run, uses 25% CPU power!

Do you have any idea about what might cause the loop below to take no less than 12 seconds to run?

Code:
Dim iRev As Long
For iRev = ActiveDocument.Range.Revisions.Count To 1 Step -1
    With ActiveDocument.Range.Revisions(iRev)
    End With
Next iRev

MsgBox ("Finally, the code has executed after 12 seconds...!")
The document has 100 revisions, and I'd like to point out that the 12s execution time is for the code above, as it is written! So, there are no other instructions, or properties to set inside the loop (that could understandably slow it down).

Moreover, the code uses 25% CPU power (winword.exe process).

I run it under Word 2016, on a Core 2 Quad 2.5 Ghz computer, and I'm using the code in a larger macro. All the other subroutines run very fast, except this one which takes so much time to execute. I made sure to test it in its own standalone subroutine to make sure it does not interfere with any other code.

Any idea about what might be the problem, or where I should look for a possible solution to speed it up? I guess it should run in well under a second.



Thank you!

Alex
Reply With Quote
  #2  
Old 10-17-2020, 04:21 AM
Boris_R Boris_R is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows Vista Short loop takes no less than 12s to run, uses 25% CPU power! Office 2010 32bit
Novice
 
Join Date: Oct 2015
Posts: 4
Boris_R is on a distinguished road
Default

Try

Code:
Dim iRev As Long, Revisions_Count As Long
Revisions_Count = ActiveDocument.Range.Revisions.Count
    For iRev = Revisions_Count To 1 Step -1
        With ActiveDocument.Range.Revisions(iRev)
        End With
    Next iRev
The property "ActiveDocument.Range.Revisions.Count" is evaluated once before entering the loop
In your code, this property is evaluated 100 times on each loop.
Reply With Quote
  #3  
Old 10-17-2020, 05:01 AM
alex100 alex100 is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows 7 64bit Short loop takes no less than 12s to run, uses 25% CPU power! Office 2016
Advanced Beginner
Short loop takes no less than 12s to run, uses 25% CPU power!
 
Join Date: May 2020
Posts: 56
alex100 is on a distinguished road
Default

Quote:
Originally Posted by Boris_R View Post
The property "ActiveDocument.Range.Revisions.Count" is evaluated once before entering the loop
I tested your code, but I'm afraid there's no improvement at all. The script takes the same amount of time to execute.

Alex
Reply With Quote
  #4  
Old 10-17-2020, 03:49 PM
Guessed's Avatar
Guessed Guessed is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows 10 Short loop takes no less than 12s to run, uses 25% CPU power! Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,936
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

Putting the with/end with inside the loop doesn't look particularly efficient so I tried putting it outside and got significant speed improvements. It still doesn't run as fast as I would like but it's better than nothing.
Code:
Sub aTest()
  Dim iRev As Long

  With ActiveDocument.Range.Revisions
    For iRev = .Count To 1 Step -1
      If iRev Mod 10 = 0 Then Debug.Print iRev, .Item(iRev).Type
    Next iRev
  End With
  MsgBox "Stage 1 done"

  For iRev = ActiveDocument.Range.Revisions.Count To 1 Step -1
      With ActiveDocument.Range.Revisions(iRev)
        If iRev Mod 10 = 0 Then Debug.Print iRev, .Type
      End With
  Next iRev
  MsgBox "Stage 2 done"
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 10-17-2020, 09:21 PM
macropod's Avatar
macropod macropod is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows 10 Short loop takes no less than 12s to run, uses 25% CPU power! Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,489
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

With Word 2010 on my laptop, using:
Code:
Sub Test()
' Dimension Variables
Dim eTime As Single, Rvn As Revision, i As Long
' Start Timing
eTime = Timer
With ActiveDocument.Range
  For Each Rvn In .Revisions
    With Rvn
    End With
  Next
  ' Calculate elapsed time
  eTime = (Timer - eTime + 86400) Mod 86400 ' Just in case execution time spans midnight
  MsgBox "Execution took " & Format(eTime / 86400, "hh:mm:ss")
  ' Start Timing
  eTime = Timer
  For i = 1 To .Revisions.Count
    With .Revisions(i)
    End With
  Next
  ' Calculate elapsed time
  eTime = (Timer - eTime + 86400) Mod 86400 ' Just in case execution time spans midnight
  MsgBox "Execution took " & Format(eTime / 86400, "hh:mm:ss")
  ' Start Timing
  eTime = Timer
  For i = .Revisions.Count To 1 Step -1
    With .Revisions(i)
    End With
  Next
  ' Calculate elapsed time
  eTime = (Timer - eTime + 86400) Mod 86400 ' Just in case execution time spans midnight
  MsgBox "Execution took " & Format(eTime / 86400, "hh:mm:ss")
End With
End Sub
the first loop took less than 1 second, the next two took about 1 second each.

Note: It's hardly surprising that a VBA routine might use about 25% (and no more) of a quad-core CPU's capacity.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 10-18-2020, 03:58 AM
alex100 alex100 is offline Short loop takes no less than 12s to run, uses 25% CPU power! Windows 7 64bit Short loop takes no less than 12s to run, uses 25% CPU power! Office 2016
Advanced Beginner
Short loop takes no less than 12s to run, uses 25% CPU power!
 
Join Date: May 2020
Posts: 56
alex100 is on a distinguished road
Default

Andrew, Paul, thank you both very much!

I tested your solutions, and in my case, the only one that did improve the execution time was Paul's first loop.

So, for the sake of clarity, here's the code I was using...

Code:
Dim iRev As Long
For iRev = ActiveDocument.Range.Revisions.Count To 1 Step -1
    With ActiveDocument.Range.Revisions(iRev)
        With ActiveDocument.Range.Revisions(iRev)
            Select Case .Type
                Case wdRevisionDelete, wdRevisionCellDeletion
                '
                ' more code here...
                '
                .Reject
            End Select
        End With
    End With
Next iRev
Execution time: 12 seconds

And now, with a major speed improvement (thanks to Paul's code!), I'm using this...

Code:
Dim iRev As Revision
With ActiveDocument.Range
    For Each iRev In .Revisions
        With iRev
            Select Case .Type
                Case wdRevisionDelete, wdRevisionCellDeletion
                '
                ' more code here...
                '
                .Reject
            End Select
        End With
    Next
End With
Execution time: 1 second

Apart from the huge difference in execution time, there are no other differences in the end result.

Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i open a power point in design mode through a power point shoe noora PowerPoint 4 12-10-2019 12:18 AM
Google Docs Power Point Short cuts / Insert Image etc Rado PowerPoint 4 04-11-2014 03:50 AM
video loop lag in power point 2013 drwrath PowerPoint 2 04-29-2013 05:53 PM
Microsoft Power Point 2004 to Office Power Point 2007 chuff PowerPoint 0 03-20-2011 01:23 PM
Short loop takes no less than 12s to run, uses 25% CPU power! mail merge takes and age williebear Outlook 1 05-27-2009 11:32 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 10:36 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft