[Year 12 SofDev] Code optimization
ATKINSON-BUCK, Damien
Damien.ATKINSON-BUCK at ivanhoe.com.au
Fri Mar 22 15:49:06 EST 2013
Thanks Chris and James, much appreciated!
Damien Atkinson-Buck
Head of Learning Area: Technology/Arts
[cid:image003.png at 01CE2714.C2A167B0]
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: Bucknell, Chris [mailto:cbucknell at whitefriars.vic.edu.au]
Sent: Friday, 22 March 2013 10:50 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Code optimization
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> [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 01CE2714.C2A167B0]
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.
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/f80d6a17/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/f80d6a17/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/f80d6a17/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/f80d6a17/image004-0001.jpg
More information about the sofdev
mailing list