.net

Code Camp 2010.1

When: 
Saturday, April 17, 2010 - 8:00am - 5:00pm

REGISTRATION IS NOW OPEN: Register Here
 


Saturday, April 17th, 2010
9 am - 5 pm
(registration 8 - 9 am)

Home of the CS Dept. at Sennott Square

Department of Computer Science
Sennott Square
University of Pittsburgh
Pittsburgh, PA 15260

Code Camp is a free, 1-day event put on by the local Pittsburgh community to help promote software development in the community.

Code Camps have been taking place all over the country. This is Pittsburgh's version of this excellent event. The continuing goal of the Code Camps is to provide an intensive developer-to-developer learning experience that is fun and technically stimulating.

All training, slides, manuals, and demo code are provided free following the event!

Our Code Camp will be hosted at the University of Pittsburgh Computer Science Department. We have local speakers from the community who will share their technical expertise and experiences.

Devlink

When: 
Thursday, August 13, 2009 (All day) - Saturday, August 15, 2009 (All day)

http://www.devlink.net/

When:
August 13 - 15, 2009

Registration Opens:
April 1, 2009

Registration Closes:
July 30, 2009 (CLOSED)

Cost:
Standard Ticket - $100

Where:
Lipscomb University
One University Park Drive
Nashville, TN 37204

If vs Ternary: Deathmatch

1: INTRO

The Fun With the ?? Operator in C#: if { } or ?? – Which is Faster? article by Keith Elder got me thinking about the if statement and the ternary operator.

Is there a real difference or are they both the same?

To answer this question I put together a little project that tests both cases and times them.

2: CODE

using System;
using System.Diagnostics;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
 
            string[] strArray = new string[20000];
            Random r = new Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < 500; i++)
            {
                int rnum = r.Next();
                if (rnum % 2 == 0)
                {
                    strArray[i] = null;
                }
                else
                {
                    strArray[i] = rnum.ToString();
                }
            }
 
            TimeSpan ts1 = TimeSpan.Zero;
            TimeSpan ts2 = TimeSpan.Zero;
 
            for (int i = 0; i < strArray.Length; i++)
            {
                ts2 = ts2.Add(countWithTernary(strArray));
                ts1 = ts1.Add(countWithIf(strArray));
            }
 
            for (int i = 0; i < strArray.Length; i++)
            {
                ts1 = ts1.Add(countWithIf(strArray));
                ts2 = ts2.Add(countWithTernary(strArray));
            }
 
            Console.WriteLine("     if: " + ts1.TotalMilliseconds.ToString());
            Console.WriteLine("ternary: " + ts2.TotalMilliseconds.ToString());
            Console.ReadLine();
        }
 
 
        private static TimeSpan countWithIf(string[] arr)
        {
            Stopwatch sw = new Stopwatch();
            int len = arr.Length;
            int count = 0;
            sw.Start();
            for (int i = 0; i < len; i++)
            {
                if (arr[i] == null)
                {
                    count += 1;
                }
                else
                {
                    count += 2;
                }
            }
            sw.Stop();
            return sw.Elapsed;
        }
 
        private static TimeSpan countWithTernary(string[] arr)
        {
            Stopwatch sw = new Stopwatch();
            int len = arr.Length;
            int count = 0;
            sw.Start();
            for (int i = 0; i < len; i++)
            {
                count += arr[i] == null ? 1 : 2;
            }
            sw.Stop();
            return sw.Elapsed;
        }
    }
}

3: RESULTS

The output of the above code suprised me, here are the results:

Here we see that using the if statement is much faster than the ternary operator.

In release the if statement gets faster and the ternary stays about the same.

Surprising to me here was when compiled with x64 the if statement was just as slow (or even slower) than the ternary one.

In release for the x64 they're both almost the same again but the ternary operation is faster than the x86 version.

4: CONCLUSION

In conclusion i think using the if statement would be faster in most cases although in most applications the difference would be unnoticeable.

Codestock

When: 
Friday, June 26, 2009 (All day) - Saturday, June 27, 2009 (All day)

CodeStock is about Community. For Developers, by Developers (with love for SysAdmins and DBAs too!). Last year an idea started at CodeStock to mix Open Spaces within a traditional conference. This year we're going to crank things up to 11 and rip off the knob - and you're being drafted to help!

CodeStock On Web

If you've blogged about CodeStock, let us know!

Read A Letter to the Community on CodeStock 2009 by CodeStock Organizer, Michael C. Neel

Syndicate content