Wednesday, August 19, 2009

 

Oddities of Boolean evaluation in JavaScript

I had some naive notions about how JavaScript evaluated certain Boolean expressions. For example, I assumed that code like
if (x) { ... }
would always evaluate like
if (x == true) { ... }
I was wrong. For example,
if ([]) { ... }
evaluates to true, but
if ([] == true) { ... }
evaluates to false.

I wrote the following test to help me identify other perilous JavaScript irregularities.
Read the squares as:
if (y)
    green
else
    red

Labels:


 

Performance of converting an integer to a string in PHP

We had a discussion at work about the fastest way to convert an integer to a string in PHP. The options were: To test the theories I ran each of those conversions a million times on PHP 5.2.8 CGI without any sort of accelerator. Casting was the fastest. Concatenation and interpolation were about same. Calling strval() took twice as long as casting. The results were:
  1. stringCasting: 0.744 s (e.g. (string)$i)
  2. stringConcatenation: 0.971 s (e.g. $i . '')
  3. stringInterpolation: 1.005 s (e.g. "$i")
  4. strvalConversion: 1.443 s (e.g. strval($i))
Here's the code I ran to get that output:
  1. <?
  2.  
  3. function timeTrial($functionName, $i) {
  4.     $start = microtime(true);
  5.     $functionName($i);
  6.     $totalTime = sprintf("%0.3f", microtime(true) - $start);
  7.     echo "<h3>$functionName: $totalTime s</h3>";
  8.     flush();
  9. }
  10.  
  11. function stringConcatenation($i) {
  12.     for ($j = 0; $j < $i; ++$j) {
  13.         if ($j . '' === $i . '') {
  14.             echo "This should never happen";
  15.         }
  16.     }
  17. }
  18.  
  19. function strvalConversion($i) {
  20.     for ($j = 0; $j < $i; ++$j) {
  21.         if (strval($j) === strval($i)) {
  22.             echo "This should never happen";
  23.         }
  24.     }
  25. }
  26.  
  27. function stringInterpolation($i) {
  28.     for ($j = 0; $j < $i; ++$j) {
  29.         if ("$j" === "$i") {
  30.             echo "This should never happen";
  31.         }
  32.     }
  33. }
  34.  
  35. function stringCasting($i) {
  36.     for ($j = 0; $j < $i; ++$j) {
  37.         if ((string)$j === (string)$i) {
  38.             echo "This should never happen";
  39.         }
  40.     }
  41. }
  42.  
  43. for ($k = 0; $k < 4; ++$k) {
  44.     $iterations = 1000000;
  45.     foreach (array("stringConcatenation", "strvalConversion", "stringInterpolation", "stringCasting") as $test) {
  46.         timeTrial($test, $iterations);
  47.     }
  48. }
  49.  

Wednesday, August 12, 2009

 

Backpacking Loop In the Crazies: Cottonwood, Glacier, Moose, and Campfire Lakes; Day 1

In mid-July, I and three friends went on a four day backpacking trip in Montana's Crazy Mountains.  Originally, we'd wanted to head to Glacier Park, but bailed on that idea when we realized what a hassle getting the back country camping permits was.    Instead, I went googling and found this guy who proposed a loop in the Crazies.  It looked fantastic.

Sadly, Beartooth Publishing hasn't released a map of the Crazies yet, so we ordered a custom topo from mytopo.com, which was high quality and beautiful and huge for about $30.  If you buy from them, remember to click through a discount link, such as the ones at Primal Quest.

We left work about noon and began the two hour drive to the Cottonwood Creek trail head.  You might not expect that the drive from Bozeman would take two hours, but the last few miles of the road are in pretty rough shape and are best taken around 10 MPH.  The parking lot was empty when we arrived.  As we were unloading our packs, a car from Utah pulled up, two folks got out, and headed up the trail, which of course disappointed us as we expected to be alone.  

In the parking lot, we weighed our packs.  The lightest was just under 40 pounds; the heaviest, mine, was just over 50 pounds, including 3 liters of water.

As we headed out of the parking lot, we met the the folks from Utah who were headed back to their car.  About three miles later we ran into this waterfall on Cottonwood Creek.  Just before the waterfall, the trail becomes poorly defined and follows an old stream bed.  We found that while the water was relatively high, it's best to stay on the downhill side of the creek and follow it upstream to a big log jam just downstream of the waterfall, which seems to be the easiest place to cross.
DSC_8719.JPG

DSC_8727.JPG
From the waterfall, the terrain ramps up and the trail hopscotches upward through an odd combination of exposed rocks and thick trees.  There was still a fair amount of snow at places in the trees.  As we neared the elevation of Cottonwood Lake, we came to a wide spot in the creek that I call Fake Cottonwood Lake since it had fooled me on a previous trip to Cottonwood Lake.  The snow was still many feet thick in places there and where there wasn't snow, there was standing water or really spongy, soggy soil.  Getting through there wasn't much fun. We got to Cottonwood Lake proper about 5:30.  Originally, we'd hoped to make it over the ridge between Cottonwood and Glacier on the first day, but decided it was too late and set up camp at Cottonwood.
Cottonwood Lake

Cottonwood Lake

Cottonwood Lake

Sunday, August 02, 2009

 

Reflections on America

I enjoyed this piece by former BBC North America editor Justin Webb reflecting on what America is as he departed after 7 years in the States.
In more than seven years of life in America, I have come to value - to love, actually - the stolid, sunny, unchallenging, simple virtuousness of the American suburban psyche.
...
When selling a home in America, you have to pretend that you do not live there. No, you have to pretend that no-one lives there. Or ever
...
The English understand that we are all falling down. Dust to dust, we intuit. Americans do not. They have not got there yet.
...
And yet for all the ugliness, the deadening tawdriness of much of the American landscape and the tinny feebleness of many of its politicians - for all that nastiness and shallowness and flakiness - there is no question in my mind that to live here has been the greatest privilege of my life.
...
But if Sonia Sotomayor is to make it big, there must be something creating the drive, and part of that something is the poverty of the alternative, the discomfort of the ordinary lives that most Americans endure and the freedom that Americans have to go to hell if that is the decision they take. This is the atmosphere in which Nobel Prize winners are nurtured
...
I feel crazy going back to the old world. My five-year-old daughter Clara, who is the proud owner of an American passport, agrees. She says she intends to leave home, at around 12-years-old, and return to her native land. I do not blame her. If you are willing to chance your arm, if you back yourself, if you want to live the life, America is still the place to be.
...

Saturday, August 01, 2009

 

Project Euler Problem 5 One Liner

After sitting stumped for a while on Project Euler Problem 5, I came up with this one liner. That's probably cheating.

This page is powered by Blogger. Isn't yours?