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
I wrote the following test to help me identify other perilous JavaScript irregularities.
Read the squares as:
would always evaluate likeif (x) { ... }
I was wrong. For example,if (x == true) { ... }
evaluates to true, butif ([]) { ... }
evaluates to false.if ([] == true) { ... }
I wrote the following test to help me identify other perilous JavaScript irregularities.
Read the squares as:
if (y)
green
else
red
Labels: javascript
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:
- Casting: e.g. (string)$i
- String concatenation: e.g. $i . ''
- String interpolation: e.g. "$i"
- Conversion with strval() e.g. strval($i)
- stringCasting: 0.744 s (e.g. (string)$i)
- stringConcatenation: 0.971 s (e.g. $i . '')
- stringInterpolation: 1.005 s (e.g. "$i")
- strvalConversion: 1.443 s (e.g. strval($i))
- <?
- function timeTrial($functionName, $i) {
- $start = microtime(true);
- $functionName($i);
- $totalTime = sprintf("%0.3f", microtime(true) - $start);
- echo "<h3>$functionName: $totalTime s</h3>";
- flush();
- }
- function stringConcatenation($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ($j . '' === $i . '') {
- echo "This should never happen";
- }
- }
- }
- function strvalConversion($i) {
- for ($j = 0; $j < $i; ++$j) {
- if (strval($j) === strval($i)) {
- echo "This should never happen";
- }
- }
- }
- function stringInterpolation($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ("$j" === "$i") {
- echo "This should never happen";
- }
- }
- }
- function stringCasting($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ((string)$j === (string)$i) {
- echo "This should never happen";
- }
- }
- }
- for ($k = 0; $k < 4; ++$k) {
- $iterations = 1000000;
- foreach (array("stringConcatenation", "strvalConversion", "stringInterpolation", "stringCasting") as $test) {
- timeTrial($test, $iterations);
- }
- }
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.
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.
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.