Mirage Source
http://miragesource.net/forums/

[Optimization] Optimized DoEvents
http://miragesource.net/forums/viewtopic.php?f=183&t=6078
Page 1 of 69

Author:  JokeofWeek [ Fri Jul 31, 2009 8:32 pm ]
Post subject:  [Optimization] Optimized DoEvents

As you probably know, DoEvents is a very useful function built-in to VB allowing us to give our applications some 'fresh air' when they are doing intensive operations, such as loops. DoEvents works by checking for any other event that might need to run at the time, and then runs it. The only problem is DoEvents checks many useless events that we might not even need.

Basically, we are 're-making' the DoEvents function to check only for certain messages, such as keyboard input, mouse input and painting the screen.

Just copy paste the following in any module :

Code:
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Public Const nLng As Long = (&H80 Or &H1 Or &H4 Or &H20) + (&H8 Or &H40)

Public Sub NewDoEvents()
    If GetQueueStatus(nLng) <> 0 Then DoEvents
End Sub


To make sure this actually optimized the function, I decided to do a benchmark. I opened up a blank VB app, put two controls and basically made a loop that would set the caption of one of the buttons to the current loop index. This is the code I used for the benchmark :

Code:
Dim I As Long, Tick As Long
Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
Next I
Text1.Text = (GetTickCount - Tick)

Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
DoEvents
Next I
Text2.Text = (GetTickCount - Tick)

Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
NewDoEvents
Next I
Text3.Text = (GetTickCount - Tick)


And the results were :
Code:
Test 1
No DoEvents whatsoever - 15694 Ticks
Old DoEvents - 97875 Ticks
New DoEvents - 17004 Ticks

Test 2
No DoEvents whatsoever -7238
Old DoEvents -109528
New DoEvents - 7800


So as you can see, it definitely is a fairly important improvement to the function, and should let your loops that use DoEvents run much faster while still allowing user input :)

Author:  Joost [ Fri Jul 31, 2009 10:24 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

DoEvents are generally bad and tend to cause a lot of errors.

Author:  JokeofWeek [ Fri Jul 31, 2009 10:36 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Joost wrote:
DoEvents are generally bad and tend to cause a lot of errors.


O rly? Link to an article/source? I've never had any problems with DoEvents..

Author:  Joost [ Sat Aug 01, 2009 10:04 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

http://www.codinghorror.com/blog/archives/000159.html

http://www.codinghorror.com/blog/archives/000370.html

http://www.codinghorror.com/blog/archives/000055.html

+ common knowledge

Author:  JokeofWeek [ Sat Aug 01, 2009 3:00 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

If you read, it says it is evil because it "processes all of the messages in the message queue, not just paint messages". We are fixing that by making it only handle input and repainting messages. Thus those articles don't apply on this tutorial. :)

Author:  Joost [ Sat Aug 01, 2009 5:25 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Fine, I was too lazy, and only checked one website. Regardless of that, DoEvents are bad, and I'll do my best avoiding them in my code as much as I can.

Author:  JokeofWeek [ Sat Aug 01, 2009 7:23 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Joost wrote:
Fine, I was too lazy, and only checked one website. Regardless of that, DoEvents are bad, and I'll do my best avoiding them in my code as much as I can.


DoEvents are not necessarily bad, as long as you use them properly :P But it's all good, it's a matter of coding style and opinion.

Author:  GIAKEN [ Sun Aug 02, 2009 12:33 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

The only time I'd use a DoEvents is in a main loop.

Author:  Joost [ Sun Aug 02, 2009 3:33 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

Even in that case, I'd stay away from it and use the Sleep function.

Author:  Nean [ Sun Aug 02, 2009 3:36 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

Joost wrote:
Even in that case, I'd stay away from it and use the Sleep function.


How would one use the sleep function instead? Example? Also is there any benefits, because I'd be willing to convert all my sources from DoEvents if so.

Author:  JokeofWeek [ Sun Aug 02, 2009 4:12 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

Nean wrote:
Joost wrote:
Even in that case, I'd stay away from it and use the Sleep function.


How would one use the sleep function instead? Example? Also is there any benefits, because I'd be willing to convert all my sources from DoEvents if so.


Lol, the problem with sleep is that it does not allow you to do anything else, so it is basically useless. The reason you would use DoEvent is to allow users to still have some input while your code is looping. If that is what you are aiming for, stay as far away from Sleep as you can xD.

Author:  Joost [ Sun Aug 02, 2009 8:39 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

http://www.geekinterview.com/question_details/38517

Here Ozzy stated that there are more proper way to do things and boasted that everyone should agree with him in a rather rude manner.

Author:  JokeofWeek [ Sun Aug 02, 2009 10:06 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Joost wrote:
http://www.geekinterview.com/question_details/38517


Out of curiosity, what would be your better alternative?

Author:  grimsk8ter11 [ Mon Aug 03, 2009 1:19 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Hilariously, Joke is right. DoEvents are a non-blocking way to use the "sleep" function in essence. The problems only occur when DoEvents is over used or misused. To properly use it is not easy, but in small apllications it has such a small effect on program flow. Mainly, if it is a GUI process, you should lock GUI, ect. There are many ways to stop it from being "evil" without the work of threading, which is sloppier than the devil to use in a small program.

This come from industry experience and over 10 years of programming with Visual Basic.

Author:  Dragoons Master [ Mon Aug 03, 2009 5:08 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

DoEvents were the cause of the biggest bug I've ever found in my game. It is evil. You can use it but you should be careful. I've used to get a RTE 28 (Out of stack space). But the problem was that the bug was not on the line that the DoEvents were, it was just random, so really hard to find out the bug.
Be careful.

Author:  James [ Mon Aug 03, 2009 8:18 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Cleaned. Also, can you post some examples of DirectInput or link to some articles instead of just calling people names and mentioning it offhand? Please, from now on, instead of just saying "MS Does this poorly" please cite exactly what you are talking about and offer up a viable solution to this bad way instead of raving on like a jerk.

Author:  grimsk8ter11 [ Mon Aug 03, 2009 8:21 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

From what I know, sleep stops the ENTIRE thread, not just the loop its in. Unless you multi thread VB, the main thread that everything is running through (including DirectInput and others) will be frozen as well.

For a game in Visual Basic, the MS form controls are just fine. In fact, even many [C, C++, C#, ect.] applications use polling of window objects for input. Directinput is helpful if you are using DirectX to form your GUI and not windows commands.

Author:  GIAKEN [ Sun Aug 23, 2009 12:02 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

I could see how this improved DoEvents could work, but I'm not getting any better FPS from it...I was expecting at least +100 FPS.

Author:  Robin [ Sat Sep 26, 2009 3:49 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Did my own benchmark and it's a nice improvement.

You're not gonna get over 9000 FPS just because of this, but it's a nice piece of code which you might as well use.

Thanks <3

Author:  GIAKEN [ Sun Sep 27, 2009 5:38 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

I had problems using this. I forgot exactly what they were, though...I think it was a problem with walking or something?

Author:  Robin [ Sun Sep 27, 2009 6:10 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Mouse movements don't trigger the doevents like they should.

Author:  wanai [ Wed Dec 01, 2021 9:24 am ]
Post subject:  Re: [Optimization] Optimized DoEvents

http://audiobookkeeper.ruhttp://cottagenet.ruhttp://eyesvision.ruhttp://eyesvisions.comhttp://factoringfee.ruhttp://filmzones.ruhttp://gadwall.ruhttp://gaffertape.ruhttp://gageboard.ruhttp://gagrule.ruhttp://gallduct.ruhttp://galvanometric.ruhttp://gangforeman.ruhttp://gangwayplatform.ruhttp://garbagechute.ruhttp://gardeningleave.ruhttp://gascautery.ruhttp://gashbucket.ruhttp://gasreturn.ruhttp://gatedsweep.ruhttp://gaugemodel.ruhttp://gaussianfilter.ruhttp://gearpitchdiameter.ru
http://geartreating.ruhttp://generalizedanalysis.ruhttp://generalprovisions.ruhttp://geophysicalprobe.ruhttp://geriatricnurse.ruhttp://getintoaflap.ruhttp://getthebounce.ruhttp://habeascorpus.ruhttp://habituate.ruhttp://hackedbolt.ruhttp://hackworker.ruhttp://hadronicannihilation.ruhttp://haemagglutinin.ruhttp://hailsquall.ruhttp://hairysphere.ruhttp://halforderfringe.ruhttp://halfsiblings.ruhttp://hallofresidence.ruhttp://haltstate.ruhttp://handcoding.ruhttp://handportedhead.ruhttp://handradar.ruhttp://handsfreetelephone.ru
http://hangonpart.ruhttp://haphazardwinding.ruhttp://hardalloyteeth.ruhttp://hardasiron.ruhttp://hardenedconcrete.ruhttp://harmonicinteraction.ruhttp://hartlaubgoose.ruhttp://hatchholddown.ruhttp://haveafinetime.ruhttp://hazardousatmosphere.ruhttp://headregulator.ruhttp://heartofgold.ruhttp://heatageingresistance.ruhttp://heatinggas.ruhttp://heavydutymetalcutting.ruhttp://jacketedwall.ruhttp://japanesecedar.ruhttp://jibtypecrane.ruhttp://jobabandonment.ruhttp://jobstress.ruhttp://jogformation.ruhttp://jointcapsule.ruhttp://jointsealingmaterial.ru
http://journallubricator.ruhttp://juicecatcher.ruhttp://junctionofchannels.ruhttp://justiciablehomicide.ruhttp://juxtapositiontwin.ruhttp://kaposidisease.ruhttp://keepagoodoffing.ruhttp://keepsmthinhand.ruhttp://kentishglory.ruhttp://kerbweight.ruhttp://kerrrotation.ruhttp://keymanassurance.ruhttp://keyserum.ruhttp://kickplate.ruhttp://killthefattedcalf.ruhttp://kilowattsecond.ruhttp://kingweakfish.ruhttp://kinozones.ruhttp://kleinbottle.ruhttp://kneejoint.ruhttp://knifesethouse.ruhttp://knockonatom.ruhttp://knowledgestate.ru
http://kondoferromagnet.ruhttp://labeledgraph.ruhttp://laborracket.ruhttp://labourearnings.ruhttp://labourleasing.ruhttp://laburnumtree.ruhttp://lacingcourse.ruhttp://lacrimalpoint.ruhttp://lactogenicfactor.ruhttp://lacunarycoefficient.ruhttp://ladletreatediron.ruhttp://laggingload.ruhttp://laissezaller.ruhttp://lambdatransition.ruhttp://laminatedmaterial.ruhttp://lammasshoot.ruhttp://lamphouse.ruhttp://lancecorporal.ruhttp://lancingdie.ruhttp://landingdoor.ruhttp://landmarksensor.ruhttp://landreform.ruhttp://landuseratio.ru
http://languagelaboratory.ruhttp://largeheart.ruhttp://lasercalibration.ruhttp://laserlens.ruhttp://laserpulse.ruhttp://laterevent.ruhttp://latrinesergeant.ruhttp://layabout.ruhttp://leadcoating.ruhttp://leadingfirm.ruhttp://learningcurve.ruhttp://leaveword.ruhttp://machinesensible.ruhttp://magneticequator.ruhttp://magnetotelluricfield.ruhttp://mailinghouse.ruhttp://majorconcern.ruhttp://mammasdarling.ruhttp://managerialstaff.ruhttp://manipulatinghand.ruhttp://manualchoke.ruhttp://medinfobooks.ruhttp://mp3lists.ru
http://nameresolution.ruhttp://naphtheneseries.ruhttp://narrowmouthed.ruhttp://nationalcensus.ruhttp://naturalfunctor.ruhttp://navelseed.ruhttp://neatplaster.ruhttp://necroticcaries.ruhttp://negativefibration.ruhttp://neighbouringrights.ruhttp://objectmodule.ruhttp://observationballoon.ruhttp://obstructivepatent.ruhttp://oceanmining.ruhttp://octupolephonon.ruhttp://offlinesystem.ruhttp://offsetholder.ruhttp://olibanumresinoid.ruhttp://onesticket.ruhttp://packedspheres.ruhttp://pagingterminal.ruhttp://palatinebones.ruhttp://palmberry.ru
http://papercoating.ruhttp://paraconvexgroup.ruhttp://parasolmonoplane.ruhttp://parkingbrake.ruhttp://partfamily.ruhttp://partialmajorant.ruhttp://quadrupleworm.ruhttp://qualitybooster.ruhttp://quasimoney.ruhttp://quenchedspark.ruhttp://quodrecuperet.ruhttp://rabbetledge.ruhttp://radialchaser.ruhttp://radiationestimator.ruhttp://railwaybridge.ruhttp://randomcoloration.ruhttp://rapidgrowth.ruhttp://rattlesnakemaster.ruhttp://reachthroughregion.ruhttp://readingmagnifier.ruhttp://rearchain.ruhttp://recessioncone.ruhttp://recordedassignment.ru
http://rectifiersubstation.ruhttp://redemptionvalue.ruhttp://reducingflange.ruhttp://referenceantigen.ruhttp://regeneratedprotein.ruhttp://reinvestmentplan.ruhttp://safedrilling.ruhttp://sagprofile.ruhttp://salestypelease.ruhttp://samplinginterval.ruhttp://satellitehydrology.ruhttp://scarcecommodity.ruhttp://scrapermat.ruhttp://screwingunit.ruhttp://seawaterpump.ruhttp://secondaryblock.ruhttp://secularclergy.ruhttp://seismicefficiency.ruhttp://selectivediffuser.ruинфоhttp://semifinishmachining.ruhttp://spicetrade.ruhttp://spysale.ru
http://stungun.ruhttp://tacticaldiameter.ruhttp://tailstockcenter.ruhttp://tamecurve.ruhttp://tapecorrection.ruhttp://tappingchuck.rutaskreasoning.ruhttp://technicalgrade.ruhttp://telangiectaticlipoma.ruhttp://telescopicdamper.ruсайтhttp://temperedmeasure.ruhttp://tenementbuilding.rutuchkashttp://ultramaficrock.ruhttp://ultraviolettesting.ru

Author:  wanai [ Fri Jan 07, 2022 11:02 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Econ

Author:  wanai [ Fri Jan 07, 2022 11:03 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

120.1

Author:  wanai [ Fri Jan 07, 2022 11:04 pm ]
Post subject:  Re: [Optimization] Optimized DoEvents

Bett

Page 1 of 69 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/