[Year 12 SofDev] Code optimization

ATKINSON-BUCK, Damien Damien.ATKINSON-BUCK at ivanhoe.com.au
Fri Mar 22 09:05:20 EST 2013


Hi folks,
            I've just started going through loops and leading into multi-dimensional arrays. Last night the boys did the first 2 exercises of Marks old "Arrays and Loops Exercises". In the increasing stars program, he managed to do it in 2 less lines of code and so I was curious as to which was physically faster. I put a timer class into the code to test it, and his was dramatically faster. 0.415 sec versus 0.084 sec
Anyone out there able to explain why?
Cheers
Damien

    Sub Main()

        'My version
        Dim firstTick As TickTimer = New TickTimer()
        For counter As Integer = 1 To 100
            For InnerCounter = 1 To counter
                Console.Write("*")
            Next
            Console.WriteLine()
        Next

        'Adems version
        Dim secondTick As TickTimer = New TickTimer()
        Dim star As String = "*"
        For counter2 As Integer = 1 To 100
            Console.WriteLine(star)
            star += "*"
        Next

        'display timing
        Console.WriteLine(firstTick.DeltaSeconds("Mine"))
        Console.WriteLine(secondTick.DeltaSeconds("Adems"))
        'wait til key pressed
        Console.ReadKey()
    End Sub


    Public Class TickTimer
        Public currentTicks As Long
        Public lastTicks As Long = System.DateTime.Now.Ticks
        Public retVal As String
        ''' <summary>
        ''' Calculates the seconds it took since the class was instantiated until this method
        ''' is first invoked and for subsequent calls since the previous time the method was called
        ''' </summary>
        ''' <param name="message">Message (e.g. "The last query took ")</param>
        ''' <returns>The passed string followed by the seconds: "          The last query took,     0.3456"</returns>
        ''' <remarks>To see how long it takes a method to execute instantiate this class at its
        ''' very begining and call this method just before it returns; Log the result with     Debug.Writeln or something similar</remarks>
        Public Function DeltaSeconds(ByVal message As String) As String
            currentTicks = System.DateTime.Now.Ticks
            retVal = String.Format("{0}, {1}", message.PadLeft(100), ((currentTicks - lastTicks) / TimeSpan.TicksPerSecond).ToString().PadRight(15))
            lastTicks = currentTicks
            Return retVal
        End Function
    End Class



Damien Atkinson-Buck
Head of Learning Area: Technology/Arts
p: +61 3 9490 3848

e: damien.atkinson-buck at ivanhoe.com.au<mailto:damien.atkinson-buck at ivanhoe.com.au>

f: +61 3 9490 3490

w: www.ivanhoe.com.au<http://www.ivanhoe.com.au>

[cid:image001.jpg at 01CE26DA.4A99EA10]
Please consider the environment before printing this e-mail.




Privacy, Virus and Copyright Warning

The information contained in this electronic message (e-mail), and any files transmitted with it:

* is intended for the named recipients only. If you have received this in error, please advise the sender and delete it and any copies immediately;
* Any personal information in this email must be used in accordance with the Privacy Act 1988 and this always applies even if it has been sent to you in error.
* represents the views of the sender and does not necessarily represent the views or formal advice of Ivanhoe Grammar School;
* may be subject to Copyright, so no further use should be made of it without the author's permission.

The School does not represent or warrant that the email or any files attached do not contain errors or are free from computer viruses or other defects nor does it accept responsibility for any loss or damage resulting directly or indirectly from the use of the email or any attached files.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20130321/8da5e0a1/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 51301 bytes
Desc: image001.jpg
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20130321/8da5e0a1/image001-0001.jpg 


More information about the sofdev mailing list