Value of Time versus Efficiency of Code

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
I was reading the responses at this thread and it reminded me of something I, a rookie, see quite often during searches for solutions.

Is it better to save time by writing the easy-to-implement solution to a problem or is it better to aim for the most efficient use of code possible?

Take the example from the linked thread. OP wanted to run a sub, twice per day, at 2 specific times.

The solutions suggested were:

(a) Use a 1 minute interval timer to check if the current time is equal to the required times and run the sub if yes.

(b) Use a timer with it's interval set to the exact amount of time until the action needs to be performed.


A solution to option (b) was presented but it looked like it took longer to create than the solution to option (a) would.


My question. While it may be good practice to aim for the most efficient use of code, where do you, if ever, draw the line? In my opinion, using option (a) in this instance was a quick and simple solution as 1 tick of a 'simple' timer does not stress the processor. But why does everyone push for option (b) style solutions for simple problems such as the one given in this post?

I think my time is worth a lot more than my processors, so when I can, I will utilize its ability in order to increase what I can do with mine.
 
Where you draw the line is exactly that: where YOU draw the line. There's no hard and fast rule so it's really up to the individual or perhaps the individual's boss. It also depends what you consider to be simple. For me, the solution using option (b) of yours is simple. For others, it may not be. Of course, for those others, pushing themselves to write "better" code will help them learn more in the long run and get them to a point where they consider that code to be simple too.

Apart from that, implementing a solution using option (a) of yours is not necessarily as simple as people might think anyway. The chances of a Timer that Ticks every minute landing exactly on a specific time is negligible, so you need extra logic to work out if you're past a particular time but you haven't already performed an action for that time, plus you have to check multiple times to see which, if any, you have approximately hit. If you go with option (b) then you know for a fact that you have perform an action every Tick so there's no need for any checking.
 
To me, the joy of programming has always been optimizing and refining and expanding. Back in my 8080 assembler days, we would have these contests on IRC to write the shortest and fastest little piece of code to do one moderately complex task. It was just awesome to see people coming with completely different code than you and 5 bytes less program data and 2 cycles less in execution, and the exact same output. Of course nowadays we are overboard in memory and processing power, systems are much more complex, but I still feel the need to optimize where I can. I feel a violent urge to send inflammatory e-mail to some developers every single time I see obvious useless bloat. If you find a better, more efficient way of doing something, why would you do it any other way?
 
Also, just a thought, but you could really just start a thread with an infinite loop, checking current time and then sleeping for 59 seconds. It's basically the same as a timer but you have no periodic event to handle on the UI thread. Does the sub to run even has to touch the UI at all? If not try something like this:

Dim t As New Thread(New ThreadStart(AddressOf MyPollingThread))
t.Start()

...
Dim MyTime As DateTime

Private Sub MyPollingThread()
    Do
        If DateTime.Now.ToString("t") = MyTime.ToString("t") Then
            DoSomething()
        End If
        Threading.Thread.Sleep(58000)
    Loop
End Sub
 
Last edited:
I feel a violent urge to send inflammatory e-mail to some developers every single time I see obvious useless bloat.
Can I have your email address? I just need to flame you for converting two DateTimes to Strings in order to compare their time portions. ;)
 
Hehe I guess I deserved that, but I can't do it all for the OP... ;)

EDIT: After thinking a bit more about it, how else would you do it? Convert to timespans to round down to minutes? I think converting to string is the fastest route here. There's a reason I used the shorttime format, you can't compare it down to seconds if you check it every minute. A 59 seconds interval will however guarantee that you don't miss the mark down to the minute.
 
Last edited:
Herman said:
how else would you do it? Convert to timespans to round down to minutes?
You don't need to convert anything:
If d1.Hour = d2.Hour AndAlso d1.Minute = d2.Minute Then
 
Thanks for the replies guys..


Where you draw the line is exactly that: where YOU draw the line. There's no hard and fast rule so it's really up to the individual or perhaps the individual's boss. It also depends what you consider to be simple. For me, the solution using option (b) of yours is simple. For others, it may not be. Of course, for those others, pushing themselves to write "better" code will help them learn more in the long run and get them to a point where they consider that code to be simple too.

Apart from that, implementing a solution using option (a) of yours is not necessarily as simple as people might think anyway. The chances of a Timer that Ticks every minute landing exactly on a specific time is negligible, so you need extra logic to work out if you're past a particular time but you haven't already performed an action for that time, plus you have to check multiple times to see which, if any, you have approximately hit. If you go with option (b) then you know for a fact that you have perform an action every Tick so there's no need for any checking.

I have edited this post after spotting the flaw in my approach:

I understand what you are saying.. As far as pushing myself to write better code, I tend to fall into a rut of only doing it when it is necessary, i.e only researching better ways to achieve something when my current effort can't cut it. Time usually eludes me so the quicker I can get something done with what I know, the better..

I will admit it is nice to revisit old applications every 6/12 months or so and see how my knowledge has progressed.. My first ever post on this forum back in February involved a program I have recently re-opened during some down time, and I laughed at how simple the solution to a particular problem was that forced me to shelve the project at the time, so I am sure this time next year I will look back on the projects I have active today and laugh at my past self :smile:


Where I am currently at, my first though solution to OP's task looked like so:

' timer1 interval is set to 60000
   Dim timeChk As String

   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

      timeChk = TimeOfDay
      If timeChk.StartsWith("05:30") Or timeChk.StartsWith("17:30") Then runSub()

   End Sub



As you reminded me though, The flaw timer tick is not as precise with timing so I now see I would have to improve to the following error-proof effort:

' timer interval is set to 59000, on hit - switch it to 62000 to avoid double hit and then reset to 59k on next tick
   Dim timeChk As String


   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

      If Timer1.Interval = 62000 Then Timer1.Interval = 59000

      timeChk = TimeOfDay
      If timeChk.StartsWith("05:30") Or timeChk.StartsWith("17:30") Then Timer1.Interval = 62000 : runSub()

   End Sub



but, if I was to use the above as the solution then, as you said, what's the point in ticking every 59 seconds checking for something when you could just set the interval for one long ass tick.. therefore:


 ' timer interval is set to 59000, on hit - switch it to interval equiv of 11 hours 59 mins and then reset to 59k on next tick for guaranteed landing
   Dim timeChk As String


   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

      If Timer1.Interval = 43140000 Then Timer1.Interval = 59000

      timeChk = TimeOfDay
      If timeChk.StartsWith("05:30") Or timeChk.StartsWith("17:30") Then Timer1.Interval = 43140000 : runSub()

   End Sub



In the interest of better code, is there a way through vb that I can record the processing power my code utilizes? I know i can use stopwatch to get the elapsed time but are there other measurement tools I can use to get more information? What resources does a tick of a timer use? Does it save any to tick once a minute, or once every 12 hours.. Somewhere in the background there is an interval counter active so some resources are being used no matter which way the timer is set..
 
Last edited:
Back
Top