My Cyborg Name
Wosh*, I doubt I’m really this scary.
Thanks to Kelly for the idea.
*Wosh: some combination of “woah, shit!”
Wosh*, I doubt I’m really this scary.
Thanks to Kelly for the idea.
*Wosh: some combination of “woah, shit!”
http://www.wta.org/~wta/c…ch+english+1116
Eva and I had a nice overnighter this weekend at Tonga Ridge. The trail was surrounded by tons of ripe blueberries, of which we harvested a liter. Few mosquitoes. Many yellow jackets. However, these yellow jackets were quite tame, not like the mean ones I’m used to. Once we got used to the background humming of millions of yellow jackets, the trip was quite nice!
http://www.amazon.com/exe…=books&n=507846
I just finished an excellent book, Code: The Hidden Language of Computer Hardware and Software by Charles Petzold. It’s well written, and I highly recommend it to anyone curious about digital logic, assembly language, and other topics about how computers really work. I’ve never had formal schooling in these subjects, and I don’t believe it is necessary in order to enjoy this book. I wrote an email to the author this morning.
Dear Mr. Petzold,
One fine, clear evening my wife and I were strolling around downtown Seattle. At Barnes and Noble I was lucky enough to pick up a copy of your book, “Code”.
Thank you. Thank you for writing this book! I just completed it, and am happy to say that I feel strengthened in my understanding of how computers really work. I enjoyed the examples, the humor and the simple, straightforward explanations. It is amazing to see what can be built by combining simple, tangible objects with novel ideas.
I do have a question about something you mention on page 353. Quoted: “An example: Many processors have bit-shifting instructions. As you’ll recall, these instructions shift the bits of the accumulator to the right or left. But almost no high-level programming languages include such operations.”
I believe a clarification is necessary. I’m familiar with Java, Perl, Bash Scripting, and Python. All of these high-level programming languages are quite popular, and all have bit-shifting operations.
Sincerely,
-Adam Monsen
And in a couple of hours he wrote back! What a cool guy.
Thank you very much. When “Code” was first published I figured that this was at least one book I’d never have to revise. Maybe that’s not entirely true….
When I wrote that passage, I was undoubtedly thinking in the long view. I was considering primarily the classic procedural languages such as Cobol and Fortran and Algol and Basic and Pascal and PL/I, and the many, many other languages discussed in the 785-page “Programming Languages: History and Fundamentals” by Jean B. Sammet and published a long, long time ago in 1969.
Of course, I knew at the time of one *major* exception to the “no bit-shifting” rule, and that is C (as I discuss ten pages later). It was primarily because of C that I said “almost no.” Of course, C has been a highly-influential language, as is evident with your list of bit-shifting languages. You might also have included C++ and C#.
I’m not sure how I’d change that passage today. Perhaps instead of “But almost no…” I’d say “Historically, few high-level programming languages have included such operations, although C and many recent languages influenced by C implement bit-shifting instructions.”
Charles
This book is not for the easily distracted. I had to read over some of the examples many times before I felt like I really “got” them. The effort was well worth it, and I’m glad I discovered this book! If you’re ready to dig in and really understand how the computer works, check it out.
This book might be thought of as a “beginner’s tutorial to assembly language”. Many pages are spent explaining the purpose and function of machine language and data.
Bored? Go download a free game! I’m not going to bother rating them since I like them all.
Maelstrom - (Maelstrom) A space combat game.
Project: Starfighter - (starfighter) a space arcade game.
Powermanga - (powermanga) Arcade 2D shoot-them-up game.
The Ur-Quan Masters - (uqm) a port of the classic game Star Control II.
Battle for Wesnoth - (wesnoth) a fantasy turn-based strategy game.
The short names in parentheses are the Fedora Core/Extras package names for these games. I’m sure these games are pre-packaged by/for other distros, too.
http://stellarium.sourceforge.net/
Stellarium creates the most beautiful astronomical views I’ve ever seen. Oh yeah, it’s free and it runs on just about everything.
While thumbing through a Ruby guide, I saw an example of computing factorials. Just out of curiosity, I decided to see how Ruby stacks up against C, Python, and Perl for this particular problem. Comments/suggestions/criticisms welcome. It would also be cool to see examples in other languages, so send ‘em in!
I “cheated” in Perl and C by using the GNU Multiple Precision Arithmetic Library. Just seemed easier that way. All code examples on this page are hereby released under the GPL.
Python
#!/usr/bin/python
# Computes factorial for number passed as first command
# line argument.
import sys
total = 0
def fact(n):
total = n
while n > 1:
total *= (n - 1)
n -= 1
return total
print fact(int(sys.argv[1]))
Perl
#!/usr/bin/perl # Computes factorial for number passed as first command # line argument. use Math::BigInt lib => 'GMP'; $b = Math::BigInt->new($ARGV[0]); print $b->bfac(),"\n";
Ruby
#!/usr/bin/ruby
# Computes factorial for number passed as first command
# line argument.
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
print fact(ARGV[0].to_i), "\n"
C
/*
* Computes factorial for number passed as first command
* line argument.
*
* Uses the GNU Multiple Precision Arithmetic Library (see
* http://www.swox.com/gmp/ ).
*
* The best "quick start" guide I could find was in the
* INSTALL file in the gmp tarball.
*
* Compile with the -lgmp command line switch.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int
main (int argc, char **argv)
{
mpz_t fact;
if (argc != 2)
{
printf ("Usage: %s <number>\n", argv[0]);
printf ("The factorial of <number> will be returned.\n");
return EXIT_FAILURE;
}
mpz_init (fact);
mpz_fac_ui (fact, atoi(argv[1]));
gmp_printf ("%Zd\n", fact);
return EXIT_SUCCESS;
}
Here are my one-off timings of each on my 1.8Ghz Pentium 4 desktop. To get the results, I just executed the code a bunch of times and took the best one.
$ time python/factorial/factorial.py 40 815915283247897734345611269596115894272000000000 real 0m0.039s user 0m0.024s sys 0m0.010s $ time perl/factorial/factorial.pl 40 815915283247897734345611269596115894272000000000 real 0m0.164s user 0m0.140s sys 0m0.014s $ time ruby/factorial/factorial.rb 40 815915283247897734345611269596115894272000000000 real 0m0.015s user 0m0.005s sys 0m0.007s $ time c/factorial/factorial 40 815915283247897734345611269596115894272000000000 real 0m0.006s user 0m0.001s sys 0m0.002s
Of the scripting languages, Ruby led the pack. I used the recursive factorial example right out of that Ruby guide I mentioned earlier, so it has a limit of about 2,800! or so.
So, how ’bout 100,000!? Well, first I have to rewrite the Ruby solution to be non-recursive. Sorry if I’m not using “canonical” Ruby, this is the first time I’ve written any.
#!/usr/bin/ruby
# Computes factorial for number passed as first command
# line argument. Non-recursive (iterative) version.
total = 0
def fact(n)
total = n
while n > 1
total *= (n - 1)
n -= 1
end
return total
end
print fact(ARGV[0].to_i), "\n"
And here are some more quick-and-dirty benchmarks:
$ time python/factorial/factorial.py 100000 > /tmp/python_fact.txt real 6m41.018s user 5m14.327s sys 0m18.453s $ time perl/factorial/factorial.pl 100000 > /tmp/perl_fact.txt <CTRL-C> real 63m41.182s user 48m54.874s sys 0m12.881s $ time ruby/factorial/nonrecursive.rb 100000 > /tmp/ruby_fact.txt real 9m22.536s user 6m51.273s sys 0m19.430s $ time c/factorial/factorial 100000 > /tmp/c_fact.txt real 0m3.834s user 0m3.769s sys 0m0.023s
I got tired of waiting for the Perl script to complete. No idea how/why Python nosed out Ruby in this example. /tmp/python_fact.txt, /tmp/ruby_fact.txt, and /tmp/c_fact.txt turned out identical (as expected), but since I interrupted the Perl process, I never got a result.
Basically, C is fast (duh), but I was really surprised at Ruby’s speed. Python and Ruby do something cool: whenever a number gets too large, it’s automatically converted into a bignum-kind of object. I’m not sure I’m too into Ruby’s syntax.
Those interested in how to find large factorials might enjoy this Google answer on how to compute 1,000,000!
Interesting: apparently, Ruby can be translated into C.
For others interested in multiple-language programming, check out the ACM “Hello World” project, which shows “Hello, World!” programs in many different languages.
While I’m at it…
$ time c/factorial/factorial 1000000 > /dev/null real 2m32.795s user 1m49.357s sys 0m1.374s
Fast enough for me!
Patrick rightly pointed out that Perl is not slow, and I completely agree. However, there is a loss of precision if Perl is left to handle numbers on its own. For example, the following implies something less than unlimited precision:
$ cat factorial.pl
#!/usr/bin/perl -w
use strict;
sub fact($) {
my $total = my $n = shift;
while ($n > 1) {
$total *= ($n-- - 1);
}
return $total;
}
printf("%.0f\n", fact($ARGV[0]));
$ perl t.pl 40
815915283247898008314102179727920573516005507072
Speed gains in Perl might also be achieved by not use’ing strict and by disabling warnings.
I should be more specific on the requirements of the challenge, so here goes: compute factorials of numbers while supporting as much precision as possible.
The other day, my friend Mark Aiken passed on some cool tips about flyswatting that I’d like to share with all you on the Interweb. When you go to swat a fly, don’t just slam your hand down on the thing and try to nail it. Clap your hands about four inches above and behind the fly, and it should bounce right into your trap! This works because flys take off by jumping up and backwards. Neat, huh?
I used to play a lot of computer games as a kid. As software falls to the code graveyard, fans are sometimes able to resurrect old games that would otherwise lie forgotten. As is the case with Star Control, which has resurfaced as The Ur-Quan Masters. The game is identical to the original, as far as I can tell. Nice work, team.
Eva made an awesome loaf of dill bread the other day. Yum! There’s nothing like fresh baked bread.
http://flickr.com/photos/47144261@N00/
I started posting photos on Flickr, just to try it out. They’ve got an excellent system.
Powered by WordPress