<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0"><channel><title>digitalBush</title> <link>http://digitalbush.com</link> <description>Tales of a Tormented Software Developer</description> <lastBuildDate>Tue, 09 Mar 2010 19:03:44 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/digitalbush" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="digitalbush" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">digitalbush</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item><title>Sieve of Eratosthenes in F#</title><link>http://digitalbush.com/2010/03/09/sieve-of-eratosthenes-in-fsharp/</link> <comments>http://digitalbush.com/2010/03/09/sieve-of-eratosthenes-in-fsharp/#comments</comments> <pubDate>Tue, 09 Mar 2010 19:03:44 +0000</pubDate> <dc:creator>josh</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[F#]]></category> <category><![CDATA[prime numbers]]></category> <category><![CDATA[Sieve of Eratosthenes]]></category><guid isPermaLink="false">http://digitalbush.com/?p=674</guid> <description><![CDATA[Here is the third(hopefully of many) implementations of the Sieve of Eratosthenes.  F# has been making a lot of noise lately, so let's see what's up.let findPrimes max=
seq {
yield 2
let maxSquareRoot:int = int (sqrt [...]]]></description> <content:encoded><![CDATA[<p>Here is the third(<a
href="http://digitalbush.com/2010/02/25/does-the-language-matter/">hopefully of many</a>) implementations of the <a
href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>.  F# has been making a lot of noise lately, so let's see what's up.</p><pre>
let findPrimes max=
    seq {
        yield 2
        let maxSquareRoot:int = int (sqrt (float max))
        let primes = Array.create (max+1) true
        for n in 3 .. 2 .. max do
            if primes.[n] then
                if n<=maxSquareRoot  then
                    for i in n*n..2*n..max do primes.[i] <- false
                yield n
    }
</pre><p>I struggled a bit with F#.  Functional languages have a different look and feel for sure.  The hardest part of this was that I had several ways to implement this.  I'm still not sure I chose the most optimal way since this is the slowest one so far. My first stab used lists like so:</p><pre>
let getPrimes max =
    let maxSquareRoot:int = int (sqrt (float max))
    let primes = Array.create (max+1) true    

    2::[3 .. 2 .. max]
    |>List.filter(fun n->
        if primes.[n] &#038;& n<=maxSquareRoot  then
            for i in n*n..2*n..max do primes.[i] <- false
        primes.[n]
    )
</pre><p>This version was slower than what I ultimately wrote up top using sequences, but I really like the pipe syntax.  I think both versions are very compact and easy to follow.  I'm a little bummed that both versions are slower than my C# version. If anyone can help me write a better(faster) version, I'd love the help.  I feel like I'm not seeing the full potential of the language and I'm missing something huge.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/digitalbush?a=URED0Y1gUEY:h6N1nn7DIek:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/digitalbush?i=URED0Y1gUEY:h6N1nn7DIek:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/digitalbush?a=URED0Y1gUEY:h6N1nn7DIek:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/digitalbush?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://digitalbush.com/2010/03/09/sieve-of-eratosthenes-in-fsharp/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Sieve of Eratosthenes in VB</title><link>http://digitalbush.com/2010/03/02/sieve-of-eratosthenes-in-vb/</link> <comments>http://digitalbush.com/2010/03/02/sieve-of-eratosthenes-in-vb/#comments</comments> <pubDate>Tue, 02 Mar 2010 18:55:34 +0000</pubDate> <dc:creator>josh</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[prime numbers]]></category> <category><![CDATA[Sieve of Eratosthenes]]></category> <category><![CDATA[vb]]></category> <category><![CDATA[vb.net]]></category><guid isPermaLink="false">http://digitalbush.com/?p=666</guid> <description><![CDATA[Here is the second (hopefully of many) implementations of the Sieve of Eratosthenes.  For this one I wanted to step outside of my comfort zone and do a language which I really don't care that much for.Function FindPrimes(ByVal max As Integer) As IList(Of Integer)
Dim vals = New List(Of Integer)(max / (Math.Log(max) - 1.08366))
Dim maxSquareRoot [...]]]></description> <content:encoded><![CDATA[<p>Here is the second (<a
href="http://digitalbush.com/2010/02/25/does-the-language-matter/">hopefully of many</a>) implementations of the <a
href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>.  For this one I wanted to step outside of my comfort zone and do a language which I really don't care that much for.</p><pre>
Function FindPrimes(ByVal max As Integer) As IList(Of Integer)
	Dim vals = New List(Of Integer)(max / (Math.Log(max) - 1.08366))
	Dim maxSquareRoot = Math.Sqrt(max)
	Dim eliminated As New BitArray(max + 1)

	vals.Add(2)

	For i = 3 To max Step 2
	    If (Not eliminated(i)) Then
		If (i &lt; maxSquareRoot) Then
		    For j = i * i To max Step 2 * i
			eliminated(j) = True
		    Next
		End If
		vals.Add(i)
	    End If
	Next

	Return vals
End Function
</pre><p>I won't bother explaining the algorithm or <a
href="http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/">my optimizations</a> again.  What I want to talk about is the language.</p><p>This one was easy for me; after all, it's the same .NET base libraries that I'm comfortable with.  At first I wrote the code with a bunch of "As Integer" garbage everywhere.  Once I realized I didn't need it, I went back and scrapped a bunch of the unnecessary code to create what you see above. It looks okay to me, but I still don't like some of the noise that VB requires.</p><ul><li><strong>End Foo</strong> - This has to be my number on gripe about VB syntax.  I REALLY hate the wordiness that is required to end a block of code.  To my eye it destroys the balance of whitespace in the code and makes it harder to see code depth at a glance.</li><li><strong>Proper Case Keywords</strong> - This is a minor thing, but I really dislike the proper cased keywords.  I'm used to seeing words start with a capital letter only if it's a class or public member.  For me it's still a whitespace thing and these capital letters everywhere encroach on it big time.</li></ul><p>It's not all hate from me though.  I actually enjoyed the loop syntax (minus the "Next" statement at the end).  The only reason I needed the Step statement was because I was skipping numbers. The syntax just felt natural.</p><p>In the end, it's not my favorite language, but it's not my least favorite.  I would squarely place this in the "meh" category. I would write code in VB if I needed to work in a legacy environment, but for new .NET code I would still turn to C#.</p><p><em>As always, if you see something stupid that I did then please let me know and I'll update as necessary.</em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/digitalbush?a=1uwuxPBNhdE:L_OnGWikUsI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/digitalbush?i=1uwuxPBNhdE:L_OnGWikUsI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/digitalbush?a=1uwuxPBNhdE:L_OnGWikUsI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/digitalbush?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://digitalbush.com/2010/03/02/sieve-of-eratosthenes-in-vb/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Sieve of Eratosthenes in C#</title><link>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/</link> <comments>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/#comments</comments> <pubDate>Sat, 27 Feb 2010 04:14:01 +0000</pubDate> <dc:creator>josh</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[c#]]></category> <category><![CDATA[prime numbers]]></category> <category><![CDATA[Sieve of Eratosthenes]]></category><guid isPermaLink="false">http://digitalbush.com/?p=643</guid> <description><![CDATA[In my personal quest to find out if the programming language really matters, I present to you my first piece of code.  This is my reference implementation of Sieve of Eratosthenes in my preferred language, C#.  For this and all future language implementations, I'll put the code at the top (without comments) and then narrate [...]]]></description> <content:encoded><![CDATA[<p>In my personal quest to find out if the <a
href="http://digitalbush.com/2010/02/25/does-the-language-matter/">programming language really matters</a>, I present to you my first piece of code.  This is my reference implementation of <a
href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a> in my preferred language, C#.  For this and all future language implementations, I'll put the code at the top (without comments) and then narrate my experience below.  Now, to the code!</p><pre>IList&lt;int&gt; findPrimes(int max) {
    var vals = new List&lt;int&gt;((int)(max/(Math.Log(max)-1.08366)));
    var maxSquareRoot = Math.Sqrt(max);
    var eliminated = new System.Collections.BitArray(max + 1);                        

    vals.Add(2);

    for (int i = 3; i <= max; i+=2) {
	if (!eliminated[i]) {
	    if (i &lt; maxSquareRoot) {
		for (int j = i * i; j &lt;= max; j+=2*i)
		    eliminated[j] = true;
	    }
	    vals.Add(i);
	}
    }
    return vals;
}</pre><p>I started by following the wikipedia definition and then optimized from there.</p><p><strong>Algorithm Optimizations</strong><br
/> I cut my work in half by treating the special case of '2'.  We know that 2 is prime and all even numbers thereafter are not.  So, we'll add two immediately and then start looping at 3 only checking odd numbers from there forward.</p><p>After we've found a prime, we only need to eliminate numbers from it's square and forward. Let's say we want to find all prime numbers up to 100 and we've just identified 7 as a prime.  Per the algorithm, I'll need to eliminate 2*7, 3*7 ,4*7, 5*7, 6*7, 7*7 ,8*7 ,9*7, 10*7 ,11*7, 12*7 ,13*7 and 14*7.  None of the even multiples matter (even times an odd is always even) and none of the multiples up to the square of the prime matter since we've already done those multiples in previous loops.  So really we only have to eliminate 7*7, 9*7, 11*7 and 13*7.  That's a 9 fewer iterations and those savings become more fruitful the deeper you go!</p><p>The last optimization is the square root calculation and check.  We know from above that we only need to start eliminating beginning at the square of the current prime. Therefore it also makes sense that we can stop even trying once we get past the to square root of the max. This saves a bunch more iterations.</p><p><strong>Language Optimizations</strong><br
/> Originally I had started by returning an <code>IEnumerable&lt;int&gt;</code>. I wasn't using the list you see above and instead I was using <code>yield return i</code>. I really like that syntax, but once I got to the VB.net version (Coming Soon!), I didn't have a direct translation for the yield keyword. I took the lazy route in the VB version and just stuffed it all into a list and returned that.  To my surprise it was faster!  I went back and changed the C# version above and it performed better.  I'm not sure why, but I'm going with it.</p><p>What do you think that you get when do a <code>sizeof(bool)</code> in C#?  I was surprised to find out that my trusty booleans actually take up a whole byte instead of a single bit.  I speculate that there is a performance benefit that all of your types fit into a byte level offset in memory.  I was thrilled to find out that we have a <code>BitArray</code> class that is useful for situations above when you need to store a lot of booleans and you need them to only take up a bit in memory. I'm not sure it helped anything, but I feel better knowing I'm using the least amount of memory possible. <img
src='http://digitalbush.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p><strong>Conclusion</strong><br
/> Despite the fact that I know C# really well, I'm very thrilled that I was able to learn a few things about the language.  Also, I'm really happy with the performance of this reference implementation.  On my machine (2.66 GHz Core2 Duo and 2 GB of RAM) I can find all of the primes under 1,000,000 in 19ms.   I think I've squeezed all I can out of this version.  Please let me know if you see something I missed or did wrong and I'll make adjustments.</p><p><strong>EDIT:</strong> I just added one more optimization that's worth noting.  Instead of constructing my list with an empty constructor, I can save a several milliseconds off the larger sets by specifying a start size of the internal array structure behind the list.  If I set this size at or slightly above the end count of prime numbers, then I avoid a lot of costly array copying as the array bounds keep getting hit.  It turns out that there is quite a bit of math involved in accurately predicting the number of primes underneath a given number.  I chose to cheat and just use <a
href="http://mathworld.wolfram.com/PrimeNumberTheorem.html">Legendre's constant with the Prime Number Theorem</a> which is close enough for my purposes. I can now calculate all primes under 1,000,000 in 10ms on my machine.  Neat!</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/digitalbush?a=ZufjNDlH4GM:bijYcnOKxZc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/digitalbush?i=ZufjNDlH4GM:bijYcnOKxZc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/digitalbush?a=ZufjNDlH4GM:bijYcnOKxZc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/digitalbush?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://digitalbush.com/2010/02/26/sieve-of-eratosthenes-in-csharp/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Does the Language Matter?</title><link>http://digitalbush.com/2010/02/25/does-the-language-matter/</link> <comments>http://digitalbush.com/2010/02/25/does-the-language-matter/#comments</comments> <pubDate>Fri, 26 Feb 2010 03:47:02 +0000</pubDate> <dc:creator>josh</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[c#]]></category> <category><![CDATA[language]]></category> <category><![CDATA[learning]]></category><guid isPermaLink="false">http://digitalbush.com/?p=641</guid> <description><![CDATA[For the past 5 years I've been head down in C# and I really enjoy the language.  While I consider myself a C# developer primarily, there are plenty of other languages I use on a day to day basis.C# - Server side code for ASP.NET websites, internal windows forms applications, and utility applications which load [...]]]></description> <content:encoded><![CDATA[<p>For the past 5 years I've been head down in C# and I really enjoy the language.  While I consider myself a C# developer primarily, there are plenty of other languages I use on a day to day basis.</p><ul><li>C# - Server side code for ASP.NET websites, internal windows forms applications, and utility applications which load and export data.</li><li>SQL - Database querying for CRUD operations (not so much anymore, NHibernate rocks my world) and for ad-hoc reporting.</li><li>PL/SQL - Triggers and a few user defined functions in the database.</li><li>Javascript - Client side code to make things move and to retrieve data asynchronously.</li><li>HTML - Web page markup.</li><li>CSS - Web page styling and positioning.</li><li>Regular Expressions - Pattern matching in strings.</li><li>XSLT - Transforming XML documents into other outputs.</li><li>XPath - Querying XML documents.</li><li>Batch Files/Scripts - Command line programming to automate tasks, move around the file system, and do stuff to files.</li></ul><p>There may be a few more that I'm not thinking about.  The point is that I'm not really "just a C# developer." I'm sure if you sit down and think about it, you use quite a few languages yourself. So, while I'm most comfortable in C#, I feel confident that I could bang out code in just about any language.  I'm only more comfortable with C# because it's what I've been using the most frequently as of late.</p><p>This is a personal challenge to myself.  I want to explore a few languages and see what I can learn.  The .NET (C#, VB.NET, F#) languages should be easy just because they all share the same base libraries.  Beyond that, who knows what I'll discover.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/digitalbush?a=_Xivj36_X0s:0wMR_4dUf2A:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/digitalbush?i=_Xivj36_X0s:0wMR_4dUf2A:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/digitalbush?a=_Xivj36_X0s:0wMR_4dUf2A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/digitalbush?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://digitalbush.com/2010/02/25/does-the-language-matter/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>New Project: hotlinkr</title><link>http://digitalbush.com/2010/02/01/new-project-hotlinkr/</link> <comments>http://digitalbush.com/2010/02/01/new-project-hotlinkr/#comments</comments> <pubDate>Mon, 01 Feb 2010 19:12:35 +0000</pubDate> <dc:creator>josh</dc:creator> <category><![CDATA[projects]]></category> <category><![CDATA[hotlinkr]]></category> <category><![CDATA[release]]></category><guid isPermaLink="false">http://digitalbush.com/?p=626</guid> <description><![CDATA[(Queue cheesy TV announcer voice)
Are you a javascript developer?
Do you graciously host demos of your stuff on your own website?
Have you been wondering where your bandwidth has gone?
Do your web logs show people hotlinking to your scripts?
If you answered yes to these questions, then boy do I have a product for you. It's hotlinkr; the revolutionary javascript [...]]]></description> <content:encoded><![CDATA[<p><em><img
class="alignright size-medium wp-image-627" title="career" src="http://digitalbush.com/wp-content/uploads/2010/01/career.fsu_.edu-after-300x247.png" alt="" width="300" height="247" />(Queue cheesy TV announcer voice)</em></p><p><strong>Are you a javascript developer?</strong></p><p><strong>Do you graciously host demos of your stuff on your own website?</strong></p><p><strong>Have you been wondering where your bandwidth has gone?</strong></p><p><strong>Do your web logs show people hotlinking to your scripts?</strong></p><p><strong><span
style="font-weight: normal;">If you answered yes to these questions, then boy do I have a product for you. It's </span><span
style="font-weight: normal;"><a
href="http://digitalbush.com/projects/hotlinkr/">hotlinkr</a></span></strong>; the revolutionary javascript hotlink countermeasure that's sure to make the web a better place!</p><p><a
href="http://digitalbush.com/projects/hotlinkr/">hotlinkr </a>is targeted towards hard working open source javascript developers who provide demos of their work only to have them hotlinked from people on other sites.  You could just pull the script and have the offending site lose the functionality your script was providing. <strong>What's the fun in that?</strong> Teach them a lesson about executing code which you don't have control over.  Replace the script with <a
href="http://digitalbush.com/projects/hotlinkr/">hotlinkr</a>!</p><p><a
href="http://digitalbush.com/projects/hotlinkr/">hotlinkr </a>will deface the website in one of many ways.  What you choose to do to the offending site is entirely up to you  You could be polite and just pop up a tiny alert.  You can be sneaky and overlay a div which makes nothing on the website clickable.  You could be funny and rick roll the site. Or just let the script choose at random!  The possibilities are endless! <em>(That is, if you consider 18 possibilities endless.)</em></p><p>Special thanks to:</p><ul><li><a
href="http://codeimpossible.com/">Jared Barboza</a> for contributing the fail, move, spaz and vroomvroom functions as well as several IE fixes.</li><li><a
href="http://jquery.malsup.com/">Mike Alsup</a> for contributing the rickroll function as well as being the first to try this out on a large scale.</li><li><a
href="http://www.cornify.com/">Cornify</a> for supplying us with an unlimited number of unicorns and rainbows to distribute to the offending hotlinkers.</li></ul><p>What are you waiting for?  Go check your logs and see who is hotlinking your javascript files right now!  Download now so you can take advantage of the special introductory price of <strong>free</strong>! That's right <strong>FREE! </strong><em>But wait there's more. </em> If you download now within the next 5 minutes, we'll double the offer. That's right ladies and gentlemen, download within the next 5 minutes for free and you'll be able to download a 2nd copy for the same price as the first, FREE!</p><p><em>(Some restriction apply, see site for details.  Offer may apply to certain areas.  Authors are not responsible for the rage from website owners that you will inevitably piss off.  No animals were harmed in the making of this script.)</em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/digitalbush?a=KEn7LoqFzug:ZkbQJlMHER4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/digitalbush?i=KEn7LoqFzug:ZkbQJlMHER4:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/digitalbush?a=KEn7LoqFzug:ZkbQJlMHER4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/digitalbush?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded> <wfw:commentRss>http://digitalbush.com/2010/02/01/new-project-hotlinkr/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss><!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 7/9 queries in 0.002 seconds using apc

Served from: hudsonbush.com @ 2010-03-09 19:03:48 -->
