Timing code within functions in Unity

I had to time some C# code within a function in Unity3D. Without the professional version of Unity, you don't have a profiler. The code I used was the following:

long startTime = DateTime.UtcNow.Ticks;

// some code you want to time

long now = DateTime.UtcNow.Ticks;
long tm1 = now - startTime;
startTime = now;

// more code you want to time

now = DateTime.UtcNow.Ticks;
long tm2 = now - startTime;
startTime = now;

// etc
Debug.Log("tm1 = " + tm1);
Debug.Log("tm2 = " + tm2);

This worked for me and enable me to determine which part of the code was taking the time (not the part that I was expecting!).

There may be better ways to do this, but this worked for me.

Reflections on Google Code Jam

Last weekend I competed in the qualification round of Google Code Jam. I went into it cold (i.e. not having read any of the previous problem sets), and found it a little harder than I expected. The first 2 questions were easy. The last 2 were easy in principle, but I found my implementation didn't scale well to the large data sets, given the limits involved. I still got well over the required score to get me into the next round.

My take-aways from the process are as follows:

Continue reading “Reflections on Google Code Jam”