[Year 12 SofDev] Code optimization

ATKINSON-BUCK, Damien Damien.ATKINSON-BUCK at ivanhoe.com.au
Fri Mar 22 15:58:59 EST 2013


Thanks Peter, I hadn't thought of that, it makes perfect sense

Damien Atkinson-Buck
Head of Learning Area: Technology/Arts

[cid:image003.png at 01CE2716.2A16BCA0]

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/>



From: Coventry, Peter J [mailto:coventry.peter.j at edumail.vic.gov.au]
Sent: Friday, 22 March 2013 3:12 PM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Code optimization

Hi,

My 2c worth.

There is a small overhead in writing to the console.

Your code is writing each character separately, so 5050 writes in all.
His is writing each string, so 100 writes.

Cheers,
Peter Coventry

From: sofdev-bounces at edulists.com.au<mailto: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<mailto: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:image004.jpg at 01CE2716.2A16BCA0]
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.

Important - This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education and Early Childhood Development.




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/20130322/c5005e99/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 1024 bytes
Desc: image002.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20130322/c5005e99/image002-0001.png 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 3779 bytes
Desc: image003.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20130322/c5005e99/image003-0001.png 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.jpg
Type: image/jpeg
Size: 51301 bytes
Desc: image004.jpg
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20130322/c5005e99/image004-0001.jpg 


More information about the sofdev mailing list