Saturday, March 16, 2013

DateTime.Now vs DateTime.UtcNow

Please note that this post does not take into account any other perspective of when to use DateTime.Now or DateTime.UtcNow other than performance.

This morning I tried to find numbers that compare DateTime.Now and DateTime.UtcNow but all I found was long discussions on the subject of when to use them. So I decided to write a quick sample myself to see.

Last value was 2013-03-16 12:50:18
DateTime.Now:           00:00:06.6733481 for 10000000 iterations
Last value was 2013-03-16 11:50:18
DateTime.UtcNow:        00:00:00.0741262 for 10000000 iterations
DateTime.UtcNow is 1,11% of DateTime.Now




   1:  class Program
   2:  {
   3:      static void Main(string[] args)
   4:      {
   5:          const int iterations = 10000000;
   6:          DateTime dt = DateTime.UtcNow;
   7:          Stopwatch sw = Stopwatch.StartNew();
   8:          for (int i = 0; i < iterations; i++)
   9:              dt = DateTime.Now;
  10:          sw.Stop();
  11:          Console.WriteLine("Last value was {0}", dt);
  12:          Console.WriteLine("DateTime.Now:\t\t{0} for {1} iterations", sw.Elapsed, iterations);
  13:          double nowMs = sw.ElapsedMilliseconds;
  14:          sw = Stopwatch.StartNew();
  15:          for (int i = 0; i < iterations; i++)
  16:              dt = DateTime.UtcNow;
  17:          sw.Stop();
  18:          Console.WriteLine("Last value was {0}", dt);
  19:          Console.WriteLine("DateTime.UtcNow:\t{0} for {1} iterations", sw.Elapsed, iterations);
  20:          double utcNowMs = sw.ElapsedMilliseconds;
  21:          Console.WriteLine("DateTime.UtcNow is {0:0.00}% of DateTime.Now", CalcPercentage(utcNowMs, nowMs));
  22:          Console.ReadLine();
  23:      }
  24:      private static double CalcPercentage(double value, double max)
  25:      {
  26:          double p = (100.0 / max);
  27:          return p * value;
  28:      }
  29:  }

Tuesday, October 11, 2011

Thread Pooling work items in C#

Sometimes you want to perform a work item in another thread but not go through the hassle to create a new thread. A good way to solve the problem is to use the ThreadPool.
using System.Threading;
 
namespace dsc.tools
{
    public class ThreadPooling
    {
        public void StartWorkItem()
        {
            ThreadPool.QueueUserWorkItem(context =>
            {
                // do stuff here
            });
            // or
            ThreadPool.QueueUserWorkItem(DoWork);
        }
 
        private void DoWork(object context)
        {
            // do stuff here
        }
    }
}

Monday, June 13, 2011

MongoDB C# Driver, Querying multiple fields with intervals

Simple way of chaining together a query when looking for records that require both field one and field two to be in a certain interval.
double foo = 1.0;
double bar = 2.0;
MongoCollection<BsonDocument> items = db.GetCollection<BsonDocument>("collection");
var query = Query.And(
    (Query.GT("field_one", foo).LT(bar)),
    (Query.GT("field_two", foo).LT(bar))
    );
MongoCursor<BsonDocument> dbResult = items.Find(query)
    .SetLimit(count);