Be kind to your rear

 

Crappy Desk Chair

Does this resemble the chair that you sit in when you code?

If you live in the same dorms as I do then odds are good.  It exactly resembles my chair, because it is my chair.  And I hate it.  Hate it so much.  Actually it isn’t the chair that I hate, it is the numb ass and sore back that come from sitting in it.  Laying on my bed to code isn’t much better, because I find it impossible to concentrate laying down.  If there was some way I could hook up my laptop to a treadmill and pace then my coding would easily improve two-fold.  But I digress (actually I have done nothing but digress, but that is par for my posts).

The point is: if you aren’t comfortable then it will affect your code and thinking ability. If you get pulled out of the coding zone because your leg fell asleep that is detrimental to your code.

If your workplace gives you crappy chairs then that should be a good indicator of how much they value you.  A good chair costs a few hundred dollars, lasts for years and can greatly affect your performance.  Any business that can’t put forth the cash to make you happy (well at least your butt) probably doesn’t care enough to hire quality coders.

Hint: you may want to do some self evaluation to make sure you don’t belong there.  Statistically speaking though half the programmers are in the bottom 50% of all programmers.  Perhaps obvious, but still worth considering.  Now a good chair won’t make a bad programmer a great one, but a bad chair will make a great programmer sore, irritable and less effective at his job.

Overall message: take care of your rear and it will take care of you, kind of.  It won’t distract anyways, as long as you stay away from Taco Bell.

Global Warming My Ass

 

Snow in September

Snow in September

 After one of the coldest summers ever, we get snow and it isn’t even October yet.  I call bullshit on global warming.

Spore

Finally the long awaited day:  Spore is here!  I am sitting here staring at the EA Download Manager just waiting for it to let me in.  Last night felt like Christmas eve when I was six all over again.  Pretty safe to say I have never been this excited about any game, or movie for that matter.  Several web sites had reviews up but it doesn’t matter what they say, nothing could deter me at this point.  Might post again after some play but more likely will be sucked into the game all day.

A simpler web browser

For a while I have had the idea that someone ( possibly me ) should make a simpler, more user friendly web browser.  Something that our grandparents could use.  Not that all web browsers are bad at this but one designed to be simple could be even better.  One of the greatest tenants for programming is to remove as much as possible, or minimilism.  I strongly believe that most software could benefit from this.

Windows Really Good Edition IE 7

The browser would still have to surf the web obviously.  That means certain functionality would need to be present.  A back button is required, forward as well.  I would like to see some data on how much the forward button is actually used before including it honestly but as a convention it should be there.  There has to be a bar to enter the url and a “go” button.  Tabs are pretty much standard nowadays and you can’t live without bookmarks/favorites/whatever they are called in your browser.  A quick search is nice as well but you could always navigate there by hand.  That list covers well over 90% of what you do in the average internet session.  And minimilism says that we may as well pull out anything that isn’t on the list.  Note: making web pages isn’t a normal activity for most of us.

Thankfully I don’t have to write this browser ( and you should be equally grateful you don’t have to use it ).  Google once more comes to the rescue with Google Chrome.  It is literally all those things plus a few other cool features, like all the tabs run as their own process so one rogue page can’t take down the entire browser.  In fact they were so psyched about it that they made it open source and had a comic drawn up to explain it, which goes into detail on the features.  The comic is available here.

The first thing that struck me is how clean it looks.  Not surprising when you consider Google’s homepage but still it is nice to not be molested with an annoying homepage when you first install it.  It imported all my bookmarks from IE ( both of them ) but didn’t check any other browsers.  When you hit a new tab you get a page with nine spots for your most commonly used pages ala Opera.  Probably the slickest thing so far is the url bar.  By default if you just enter plaintext it will take you to the Google results page for that search.  Nice since otherwise it wouldn’t have a one click search.  It also greys out the part of the url that isn’t the main page, which is aesthetically nice but also functional in allowing you to figure out where you are at a glance.

Google Chrome

Have only used it for about an hour ( mostly to type this post ) so we will see if it has any other quirks.  About the worst thing I can come up with is the logo looks like a funky Poke ball on my toolbar.

Google Chrome logoOh and yes I am still alive, more posts to come.

Big O Notation

It is time for a primer on big o.
Big O

No, not that Big O. I am talking about the one we use in computer science. (If you don’t undestand the picture it is from an anime called The Big O)

What is big o notation?

Big O notation is a method used in computer science, and mathematics, to describe the efficiency of an algorithm. There are several other notations but big o is the most common for computer science. In short it is a function that describes the worst case performance of an algorithm. Big o means given n inputs the algorithm uses O(x), pronounced O of x, resources. x is a function that depends on n. Generally we are measuring operations, and thus speed, but it can also be used for memory usage or many other metrics.  Big o is useful because it tells us the algorithm will never be any worse than this. Little o tells you it can never be better than this and theta notation says it will be in this family, but those are beyond the scope of this post.

Figuring out big o

Actually proving some algorithm belongs to a certain set of big o families is not always easy but it is usually pretty simple to ballpark it. There are several commonly used types that we will cover.

Constant Time

Let’s look at a simple function, it just returns a value from an array given the index.  All the examples are going to be in C++, just a forewarning.

int getIndex( int Data[], int Index )
{
   return Data[Index];
}

This will take the same amount of time no matter what values we give it. We call this constant time or O(1). Now for a more interesting example.

Logarithmic Time

Logarithmic time is most often found where you partition the data into two even parts then continue working on one of them.  Binary search is a good example of this, it looks for a value in a sorted array.  It chooses a pivot value in the middle, determines if the value it is looking for is above or below the pivot then starts the search again using the new partition.

int binarySearch(int sortedArray[], int first, int last, int key) {
   while (first <= last) {
      int mid = (first + last) / 2; // compute mid point.
      if (key > sortedArray[mid])
         first = mid + 1; // repeat search in top half.
      else if (key < sortedArray[mid])
         last = mid – 1; // repeat search in bottom half.
      else
         return mid; // found it. return position /////
   }
return -(first + 1); // failed to find key
}

The proof showing that the relation between amount of work down and the number of inputs is not trivial so I will just say that these types of algorithms are classified as O(log n).  (Look up master theorem if you need more nitty gritty)  Technically it should be log base 2 n but few people complain if you just say log n.

Linear Time

Here is a function that takes an array of ints and prints them out.

void printInts( int Data[], int Size )
{
   for( int i = 0; i < Size; i++ )
   {
      std::cout << Data[i] << std::endl;
   }
}

We would say that Size is n because that is how many elements we have to look at. Since there is no way to get the job done without printing each of the n elements this is O(n) or linear time.

Polynomial

If you take a loop that is called n times and place a loop that does an additional n work what do you get? You get polynomial time.

void printAll( int Data[][], int SizeOne, int SizeTwo )
{
   for( int i = 0; i < SizeOne; i++ )
   {
      for( int j = 0; j < SizeTwo; j++ )
      {
         std::cout << Data[i][j] << ” “;
      }
      std::cout << std::endl;
   }
}

If we say SizeOne is n and SizeTwo is m, then we do n work, but each n work consists of m work as well. So we have to do n*m work. If n = m we would get n*n or n^2 work. Polynomial time is anything of the form n^m, where m doesn’t have to be an integer but it is greater than one. O(n^2) is referred to as quadratic because it appears often enough to get a special name.  This is written as O(n^2), O(n^3), etc.

Others

Exponential is O(c^n), where c is some some constant value. Examples that use this would be the traveling salesman problem, a notoriously hard problem in computing.

N to the n is O(n^n), something you should avoid writing if at all possible. These are slow. And by slow I mean the universe will end before this code finishes running.

In summary here are the most common forms and their names.
Big O Notation table

Why You Care

If each operation takes one second, then with n at 1 million this is how long it takes for each of the common ones to run.

Big O Timing

It should be obvious from the chart that a small change in the big o value of your algorithm can make a huge difference in computation time.  For most problems you can find something that is polynomial or better, log-linear being the best you can get for many problems.

Big o is also the worst case performance.  So getting O(n^2) may only occur when all the ducks are in a row, average case may be O(n log n) which is much faster for large n.  Another thing to consider is while many algorithms may have the same big o implementations affect performance as well.  So for small data sets a lean O(n^2) algorithm may out perform a O(n log n) one that has large amounts of overhead.  All things to keep in mind when you are writing code.