Science, it still works!


Today I stumbled upon the following article. I encourage you to read the article, but for brevity I will sum it up. Basically, when creating a for loop in most C-based languages, there are two basic incrementation operators: Pre-increment (++i) and Post-increment (i++), with Post-increment being by far more common. From a functional standpoint, in C# they are equivalent within a loop and always produce the same output. The premise of the article is that while Post-incrementing is recommended by most computer science professors, due to it’s implementation in the ECMA standards it is actually slower than Pre-incrementation.

I tend to be a skeptic, so when I read the article I thought the author must have been mistaken. After all, I assumed that computer science is a field where something as common as the for loop would be well documented and understood by the academics. In order to test the author’s claim I decided to write some code to test how long it took each operator to iterate 1 billion times incrementing a variable on each iteration. Here is the code:

using System;

namespace PostIncrementTest {
    class Program {
        static void Main(string[] args) {
            const long reps = 1000000000;
            for (int k = 0; k < 10; k++) {
                long temp = 0;
                // Define start time
                DateTime firstLoopStart = DateTime.Now;
                // Do a Post-Increment Loop
                for (long i = 0; i < reps; i++) {
                    temp++;
                }
                // Define end time
                DateTime firstLoopEnd = DateTime.Now;
                temp = 0;
                // Define start time
                DateTime secondLoopStart = DateTime.Now;
                // Do a Pre-Increment Loop
                for (long i = 0; i < reps; ++i) {
                    ++temp;
                }
                // Define end time
                DateTime secondLoopEnd = DateTime.Now;
                TimeSpan firstLoopTime = firstLoopEnd - firstLoopStart;
                TimeSpan secondLoopTime = secondLoopEnd - secondLoopStart;
                Console.WriteLine("The post-increment loop took {0} seconds", firstLoopTime.TotalSeconds);
                Console.WriteLine("The pre-increment loop took {0} seconds", secondLoopTime.TotalSeconds);
            }
            // Show that the operators produce the same output
            for(int i = 0; i < 10; i++)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            for (int i = 0; i < 10; i++) {
                Console.Write(i + " ");
            }
            Console.ReadLine();
        }
    }
}

And the results:

Results of the test, pre-increment is very slightly faster

While there is a measurable difference between the two operators, it is so minute that over the course of 1 billion iterations it only amounted to .02 seconds difference on average on my machine. In a loop used in an every day program, this would most likely make no measurable difference. Although the difference was minute, I may still start using the Pre-increment operator since it is such a small change.

Advertisements

Getting Started with Project Euler


If you haven’t already, go ahead and sign up at Project Euler. Once you have logged in, head on over to the Problems page. I recommend sorting the problems by ascending difficulty, as this will allow you to tackle the problems in a (more or less) gentle slope, at least at first. Let’s take a look at the first problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

So where do we get started? The key to this problem is the modulo operator. For those who are unfamiliar with the modulo operator (notated in many languages as % ), it divides two numbers and returns the remainder. So, if a number divides evenly by 3 or 5, we want to add it to a running total. So, if we were to look at this problem in language-agnostic pseudo-code we would get:

for each number between 0 and 1000
     if number/3 evenly, or number/5 evenly
          add the number to a running total
print the total to the user

With that, you should be able to complete this problem fairly quickly. If you would like to see this problem implemented in a few programming languages, hit the Read link below:
(more…)

Using SQLite with C#


Overview

Adding a database to your application can be an easy way to store data and settings between sessions for your program, but it is not always feasible to use a server based DBMS to store your database. SQLite is a small, fast, and reliable database which can be used without the end user having to install anything extra (achieved by referencing a single .dll in your project). There are a few things we as developers must do to get started with SQLite:

(more…)