Is Twitter Good for Developers -- and DateTime.Now

Over the last nearly four years on Twitter, I have learned much. Whether it was something like a code snippet, or a valuable link to an article or blog post, or just some quick information, Twitter has earned the right to be my useful companion during working hours.

I guess the real question is "Is social networking good for developers?". I can only offer my personal observation that indeed, it is - when used properly.

Many companies have corporate firewall policies that block social networking sites like Twitter and Facebook. I guess the rationale is that we must prevent clear - thinking, responsible adult employees from exercising free will and educating themselves, which can of course be of direct benefit to the company that employs them. They assume, largely incorrectly, that employees will abuse this freedom and spend hours online with their friends to the exclusion of the work they're being paid to do for the company.

While there are certainly a few deadbeat employees in any organization that will inevitably do this, if you take Twitter away then the deadbeats will simply do something else - like playing Solitaire instead of working. The lesson here is that if you are a company that wants its employees to grow and contribute more value, blocking all of them from social networking sites because of a small number of losers  is simply shooting yourself in the foot. Essentially, the "Nanny State Mentality".

When I first signed up for Twitter in November of 2007, I determined almost immediately that I would use it primarily to follow other .NET developers, which is what I still do today, and of course most of my followers are other developers.

Corporate firewalls have never stopped me from using Twitter. I just switch to a desktop third party client such as Seesmic, which they usually are not smart enough to block. And if they blocked that, then I'd just go ahead and write my own online Twitter client using Twitterizer, TweetSharp, Linq to Twitter, or any one of a number of quality Twitter API client libraries.

Because I primarily follow other developers, this has shaped my Twitter stream over time to almost always contains information and links that are useful to me.  Here is an example.

David Penton, a .NET and SQL Server dev from Dallas, tweeted:

"I'm looking through a method right now where someone had used DateTime.Now 15 times. In one method."

Interested, I tweeted back:

" -- what's wrong with that?"

David explained:

"if you only need to call it once, why call it many times? Plus, lots of times you can use UtcNow which is about 30x faster."

I agreed, tweeting:

"if you store it in a variable it will be the same value throughout. Agreed. Though calling DateTime.Now incurs little overhead."

Then Keyvan Nayyeri chimed in:

"DateTime.Now is basically expensive in its internal working. There is a discussion about it in CLR via C#."

I asked Keyvan to provide a link, and he did. So here is what I learned from this short interchange on Twitter:

Contrary to my original assumption, DateTime.Now is indeed expensive, especially when called repeatedly in a loop. Take a look at what it disassembles to from Reflector:

public static DateTime Now
        DateTime utcNow = UtcNow;
        bool isAmbiguousLocalDst = false;
        long ticks = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utcNow, out isAmbiguousLocalDst).Ticks;
        long num2 = utcNow.Ticks + ticks;
        if (num2 > 0x2bca2875f4373fffL)
             return new DateTime(0x2bca2875f4373fffL, DateTimeKind.Local);
        if (num2 < 0L)
             return new DateTime(0L, DateTimeKind.Local);
        return new DateTime(num2, DateTimeKind.Local, isAmbiguousLocalDst);

You can see above that there is a lot of stuff going on there just to return local time.  By contrast, let's have a look at DateTime.UtcNow:

// This is about 117 times faster than DateTime.Now above
public static DateTime UtcNow
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries"), SecuritySafeCritical]
        return new DateTime((ulong) ((GetSystemTimeAsFileTime() + 0x701ce1722770000L) | 0x4000000000000000L));

On my machine, running 1 million iterations of each of the above, DateTime.UtcNow is about 117 times faster: 12 ms vs 1,412 ms. And the fact of the matter is, the only situation where you would really need local time is for display to the user, which almost never happens in "a loop".

Another example: I was having difficulty getting XDebug working with JetBrains PHPStorm. I tweeted asking for help. Both  Nikolay Matveev of Jetbrains and Brad Bell responded with help. Brad even emailed me his php.ini file and a couple of configuration screenshots. His settings worked perfectly the first time, saving me hours of toil and frustration. Twitter has come to the rescue dozens of times.

Over the last nearly four years on Twitter, I have learned much. Whether it was something like the above, or a valuable link to an article or blog post, or just some quick information, Twitter has earned the right to be my useful companion during working hours. It's also a good tool for sharing information with like-minded folks. Facebook is also a useful tool, but, like Twitter, it requires that you carefully build a cadre of friends who are meaningful to what you do.

You follow?

By Peter Bromberg   Popularity  (4300 Views)