[Year 12 SofDev] Code optimization

Bucknell, Chris cbucknell at whitefriars.vic.edu.au
Fri Mar 22 10:50:19 EST 2013


Hi Damien,

It could be due building a managed app with the .Net framework.  The framework does a whole heap of things for us (especially garbage collection and thread management), so a simple timing test like you've used can return some strange results.  I've included an extract from a text book that explains time test in the .Net environment (and gives you code for a timing class).

Hope this is of some use.

Regards Chris

From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of ATKINSON-BUCK, Damien
Sent: Friday, 22 March 2013 9:05 AM
To: Year 12 Software Development Teachers' Mailing List (sofdev at edulists.com.au)
Subject: [Year 12 SofDev] Code optimization

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 01CE26E9.92562E40]
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/fa78ef6c/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/fa78ef6c/image001-0001.jpg 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Timing Tests for the .NET Environment.docx
Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Size: 34589 bytes
Desc: Timing Tests for the .NET Environment.docx
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20130321/fa78ef6c/TimingTestsforthe.NETEnvironment-0001.docx 


More information about the sofdev mailing list