[Year 12 SofDev] Code Optimisation

Vella, James jvella at mackillop.vic.edu.au
Fri Mar 22 10:20:44 EST 2013


I'm guessing it's partly to do with the fact that your code has 2x FOR loops compared to his 1.

His code has only one condition to check, while your code, while providing the same functionality, has two conditions to check.

http://www.programming4scientists.com/2008/10/03/how-to-optimise-code/ - I'll admit I did cheat a bit to back my thoughts up...look at the Code re-ordering section explains the condition checking part nicely...I think.


Mr James Vella
MacKillop College

-----Original Message-----

Today's Topics:

   1. Code optimization (ATKINSON-BUCK, Damien)


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.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.jpg

------------------------------

_______________________________________________
sofdev mailing list
sofdev at edulists.com.au
http://www.edulists.com.au/mailman/listinfo/sofdev


End of sofdev Digest, Vol 96, Issue 24
**************************************


CONFIDENTIALITY NOTICE
This e-mail contains confidential information intended only for the use of the name addressee. It may also be subject to professional privilege. If you are not the named recipient any disclosure, copying, forwarding or using any part of this e-mail or its attachments is prohibited. If you have received this e-mail in error, please notify us by telephone on 0387345200. MacKillop College does not warrant that this e-mail or any attachments are error or virus free and recommends that all attachments be checked for viruses and other defects. Similarly, MacKillop College does not warrant that this e-mail has been free of interception by a third party and may store the content sent to us, if we believe that we have a legal requirement to do so. Please don't print this e-mail unless you really need to.



More information about the sofdev mailing list