Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, February 18, 2011

Ruby v. Python

I'm gonna lose 80% of my 10 readers with this.
$ uname -a
Darwin peregrine 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
$ python --version; time (python -c "print ''.join([chr(int(x, 16)) for x in '54 59 20 66 6f 72 20 74 68 65 20 75 70 76 6f 74 65'.split()])")
Python 2.6.1
TY for the upvote

real 0m0.039s
user 0m0.014s
sys 0m0.015s
$ ruby --version; time (ruby -e "puts '54 59 20 66 6f 72 20 74 68 65 20 75 70 76 6f 74 65'.split.map{|a|a.to_i(16)}.pack('c*')")
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
TY for the upvote

real 0m0.012s
user 0m0.002s
sys 0m0.003s
$
Partially [ via reddit ]

Update: Well, my friend Chuck over at evilchuck.com pointed out that my original post is a completely pointless comparison, since both interpreters are doing practically nothing.

He is absolutely right. If this were used as a "benchmark test" it would be laughable. Well I included the time stats, so I guess I asked for it.

Here is a little test program for generating N sets of random hex strings 17 characters long and then decoding the sets one at a time.

Ruby code:Blargh:
$ time ( ruby test.rb 500 )
wnATVthnfLdBCQguVr
[ Large output elided. ]
jPgUoj1VtucFgrt7iv

real 0m0.051s
user 0m0.030s
sys 0m0.005s
$ [ With n = 5000 ]
VyiKEjdJhjMKHPRqpP
[ Larger output elided. ]
Uh3 Wsg2 HUbAAe04J

real 0m0.461s
user 0m0.293s
sys 0m0.017s
$ [ With n = 50000 ]
yKfTGOrtCJTtyWvWJk
[ Even larger output elided. ]
QkxCNYwEFVund6H8kz

real 0m3.991s
user 0m3.238s
sys 0m0.136s
$ time ( ./test.rb 100000 -q )

real 0m6.531s
user 0m6.183s
sys 0m0.114s
$ time ( ./test.rb 1000000 -q )

real 1m42.502s
user 1m17.887s
sys 0m2.240s
$
And here's a test for some python code that I hacked together that is probably really crappy but seems to do the job.

Python version:Running it from my bash shell produces:
$ time ( ./test.py 500 )
lFy 7ZGqT1Oc BT31
[ Elision. ]
fnpLoKCaYFWL9U9SE

real 0m0.089s
user 0m0.047s
sys 0m0.024s
$ time ( ./test.py 5000 )
Hqa2g4iX6Nw8Nzua5
[ Elision. ]
ofMMAG WnxUEP0RVC

real 0m0.345s
user 0m0.282s
sys 0m0.030s
$ time ( ./test.py 50000 )
kVgC4etTXn5vxrLzV
[ Elision. ]
ot8PSkiWyn1KTitXT

real 0m3.402s
user 0m2.647s
sys 0m0.108s
$ time ( ./test.py 100000 -q )

real 0m4.403s
user 0m4.241s
sys 0m0.064s
$ time ( ./test.py 1000000 -q )

real 0m43.506s
user 0m42.172s
sys 0m0.565s
Clearly, Python is much faster than Ruby for large sample sizes. At 1M, Ruby finishes in 1m43s real and 2.24s sys. Python finishes in 43.5s real and 0.565s sys.

Just for fun, I tried running the ruby code with Ruby 1.9. This isn't fair since I am not going to go to the trouble of installing Python 3.0.1, but it doesn't matter. Ruby 1.9 is significantly faster than 1.8.7, but still can't beat Python 2.6 for this particular test.
$ time ( ruby19 test.rb 1000000 -q )

real 0m49.606s
user 0m47.847s
sys 0m0.764s
$
I was curious, so I tried to isolate the decoding part of the two versions. Here's what I found:
$ ruby19 test.rb 1000000 -q
user system total real
16.750000 0.310000 17.060000 ( 17.843644)
$ ./test.py 1000000 -q
1 function calls in 14.241 CPU seconds

Ordered by: internal time, call count

ncalls tottime percall cumtime percall filename:lineno(function)
1 14.241 14.241 14.241 14.241 test.py:18(decode)
$
Looks like the decode method written in Python is executing at least 3 seconds faster.

These tests are not intended to be benchmarks of any kind. True benchmarking is a science in which I have not educated myself very well. I just wanted to get a feel for some of the differences in Python and Ruby idioms and a rough comparison of their execution time. My guess is that if anyone were to take a close look at the code used in this post, they'd be able to point out several flaws and mistakes that would affect the overall statistics.

Update 2: Thought I'd try JRuby, and...
$ time ( jruby test.rb 1000000 -q )

real 0m28.038s
user 0m29.531s
sys 0m1.310s
$
Wow.

Tuesday, December 07, 2010

Why Grails sucks

Or,
Why Grails is the worst web application framework I have ever used in my life, and I've used some pretty bad ones

Here's one reason:
Compilation error: Compilation Failed
Application context shutting down...
Application context shutdown.
Soooo sorry, but that's nothing but 100% grade-A pure American male cow manure.

Friday, May 07, 2010

Sequel

Michael Granger's blog mentions this awesome little piece of ruby database modeling/persistence tech called Sequel.

Sequel appears to be the best alternative to ActiveRecord that I have ever seen. Probably.

This is how you connect to a DB:
DB = Sequel.connect('postgres://localhost/blog?user=user')
Easy-peasy.

Tuesday, June 30, 2009

What the f--- it's Tuesday

Generally speaking, I'm feeling good. I've pretty much wrapped up a project at work with three other programmers and annoying QA defects are just barely trickling in. Most is quiet on the UAT front, and release is coming up next Monday after code freezing Thursday.

I've been able to get my hands on tons of good new music lately because my rental car has XM satellite radio, and I have been keeping track of all the songs I like by jotting them down on a pad of paper on the ample arm rest. Looking those songs and musicians up on google and hypem.com have delivered an astounding crop of new-to-me music blogs and enough mp3s for days.

Cooking has been on my agenda again lately, so I've never been hungry. I've been running at least three to four miles a week and that means so has Flora. I quit smoking after Coachella so running has been getting gradually easier day by day as my lungs slowly recover. There's really no comparison at any other time in my life to how out of shape I've been. Getting out of this wretched malaise has been like pulling teeth every step of the way through quicksand.

As you already know, Michael Jackson died this week. To be honest, I never really cared for his music because I was completely prejudiced against it by his personal defects. Although, I will say that I have always enjoyed listening to Thriller and Billie Jean.

Now that he is dead, and no longer allegedly assaulting children (or the suppressed but still highly influential Presbyterian sensibilities intensely cultivated in my youth that have retarded my social capabilities) I am somehow more willing to give his music a chance. How pathetic is that? However pathetic it is or isn't, my spirit has been lifted up by such tunes as PYT, Butterflies and remixes of Billie Jean.

Sunday, I watched the movie Walk The Line for the first time and I really enjoyed it. I've always liked Johnny Cash, and was sad when he passed away in 2003, but I never knew what a troubled person he was until June helped him turn it all around. The movie may have taken liberties with the biographical facts, but it made me wonder what could have been if Elvis had someone in his life like June Carter -- someone strong and willful, seemingly so full of self-respect and dignity -- instead of his wife, Priscilla. It made me wonder about Michael, too. What if?

God bless Michael Jackson.

Thursday, May 28, 2009

can't activate rails (= 2.2.2, runtime)

Is your computer being uncooperative when you try to run rake or some other ruby or rails software?
$rake
rake aborted!
can't activate rails (= 2.2.2, runtime), already activated rails-2.3.2
From my experience, this probably means that you have installed other versions of rails or some other conflicting gems in your local gem repository (~/.gem).

How did this happen? Well, you probably installed those gems without using the sudo command.

When you install ruby gems as a user without root or admin privileges, ruby will warn you that you are not installing those gems for the entire system.
WARNING: Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /Users/nels.nelson/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
Don't worry -- you can recover from this misstep. Those locally installed gems will probably interfere with gems that have been installed system-wide. No problem.

Remove them!
$rm -rf ~/.gem/ruby/*/cache/*
$rm -rf ~/.gem/ruby/*/doc/*
$rm -rf ~/.gem/ruby/*/gems/*
$rm -rf ~/.gem/ruby/*/specifications/*
BUT WAIT!!

Don't just delete stuff because you are told to. Examine those directories, and make sure that you have already installed those gems for the entire system. If not you will have to reinstall them for the entire system if you still need to, and if you have admin privileges (you can use the sudo command).
$sudo gem install <your gem>
Now that you have removed those gems from your user-local gem repository, you should be able to actually reinstall them. This means that you should make a list of what you had previously installed.

Friday, February 20, 2009

Testing some Bash highlighting css by Nigel McNie

  1. #!/usr/bin/env bash

  2.  

  3. [ -a /etc/*-release ] && grep -q "Ubuntu" /etc/*-release   && os="ubuntu";


  4. [ -a /etc/*-release ] && grep -q "Red Hat" /etc/*-release  && os="redhat";


  5. [ -a /etc/issue ] && grep -q "Debian" /etc/issue           && os="debian";


  6. [ ! -z $(which uname) ] && uname -a | grep -q "Darwin"     && os="darwin";


  7.  

  8. case "${os}" in

  9.      darwin ) echo "Darwin!";;


  10.      ubuntu ) echo "Ubuntu!";;

  11.      debian ) echo "Debian!";;


  12.      redhat ) echo "Red Hat!";;

  13.           * ) echo "You're dumb and your computer hates you!";;


  14. esac


http://pastebin.com/f53e15b06

Thursday, November 20, 2008

Thanks Mike! (Apache Tomcat SSL Certificate Keystore Configuration)

Today at work Mike helped me out with my Tomcat SSL crap.

The Tomcat SSL Connector in the conf/server.xml file seems to want to use a java keystore file.

That keystore file is usually created with the $JAVA_HOME/jre/bin/keytool program. Unfortunately, I was given a key file by my sys admin, and keytool requires that the pem file containing the key is X.509 encoded.

The openssl program will happily take care of encoding the pem file containing the actual certificate file:
-----BEGIN CERTIFICATE-----
...asdfghjkl=...
-----END CERTIFICATE-----
Unfortunately, it doesn't seem to work for the actual key file:
-----BEGIN RSA PRIVATE KEY-----
...asdfghjkl...
-----END RSA PRIVATE KEY-----
So, I asked Mike if he happened to know anything about SSL and Java keystores. He gave me this script (slightly modified from the original):
http://pastebin.com/f73d69466

His script basically automates the process of getting a personal certificate, or rather, converting between a SSL chain certificate pem file and a Java keystore file.

It does this by first using openssl to create a PKCS12 encoded version of the pem file given piped thru stdin, and then using PKCS12Import to store the PKCS12 entry into the Java keystore. Very nice.

Further reading:
Extracting private key from Java
Some Java source code for the com.sun.xml.wss package at dev.java.net

In summary, Mike is cool. Thanks, Mike.