RSS
 

Ruby 1.9.2 great performances

06 Sep

With a previous test, I found that JRuby was the highest expression in regard to the speed in executing ruby code, outperforming not only the C + + and .Net but also comparison with the last two incarnations of python. These are the scripts.

Now that Rails 3 is released and an interpreter suggested is the new 1.9.2, I was curious to observe how the official release works: well, I would say not bad!
This time I limited the number of participants alone versions installed with RVM and a couple more versions of Windows: JRuby 1.4.0 and the most used 1.8.7 mingw32, just to have a benchmark.

Linux distribution: Ubuntu 10.4 64bit
Windows XP 32Bit SP3

Version Builder/System Seconds
Ruby 1.9.2 p0 RVM x86_64-linux 6,6
JRuby 1.4.0 RVM OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java] 6,8
JRuby 1.5.2 RVM OpenJDK 64-Bit Server VM 1.6.0_18 [amd64-java] 7,0
JRuby 1.4.0 Windows Client VM 1.6.0_15 [x86-java] 7,0
Ruby 1.9.1 p378 RVM x86_64-linux 8,9
Ruby 1.8.7 p249 RVM x86_64-linux 12,2
Ruby 1.8.7 p249 i386-mingw32 23,9

This is the complete log of the execution:


marco@d9400:~$ rvm list

rvm rubies

=> jruby-1.4.0 [ amd64-java ]
jruby-1.5.2 [ amd64-java ]
ruby-1.8.7-p249 [ x86_64 ]
ruby-1.9.1-p378 [ x86_64 ]
ruby-1.9.2-p0 [ x86_64 ]

marco@d9400:~$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1)
OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)

marco@d9400:~$ ruby -v
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast

Warming up...
Strings test - Elapsed 2.096
Check1: 50000
Check2: 157490
Check3: 50005
Check4: 373847
Arrays test - Elapsed 3.508
Check1: 20900
Check2: 250000
Check3: 250000
Check4: 1500000
Check5: 5749950
Numeric test - Elapsed 2.856
Check1: 6252500
Check2: 1439295494700374021157505910939096377494040420940312
Ruby Partial elapsed time 8.460

1. Starting Ruby tests...
Strings test - Elapsed 1.501
Arrays test - Elapsed 3.054
Numeric test - Elapsed 2.280
Ruby Partial elapsed time 6.835

2. Starting Ruby tests...
Strings test - Elapsed 1.431
Arrays test - Elapsed 3.091
Numeric test - Elapsed 2.249
Ruby Partial elapsed time 6.771

3. Starting Ruby tests...
Strings test - Elapsed 1.505
Arrays test - Elapsed 2.999
Numeric test - Elapsed 2.261
Ruby Partial elapsed time 6.765
-------------------------------------
Average Strings test - Elapsed 1.479
Average Arrays test - Elapsed 3.048
Average Numeric test - Elapsed 2.263

Ruby Average elapsed time 6.790

marco@d9400:~$ rvm use jruby-1.5.2
marco@d9400:~$ ruby -v
jruby 1.5.2 (ruby 1.8.7 patchlevel 249) (2010-08-20 1c5e29d) (OpenJDK 64-Bit Server VM 1.6.0_18) [amd64-java]
marco@d9400:~$ ruby RubyMM186_3.rb --server --fast

Warming up...
Strings test - Elapsed 2.383
Check1: 50000
Check2: 157490
Check3: 50005
Check4: 373847
Arrays test - Elapsed 3.561
Check1: 20900
Check2: 250000
Check3: 250000
Check4: 1500000
Check5: 5749950
Numeric test - Elapsed 3.210
Check1: 6252500
Check2: 1439295494700374021157505910939096377494040420940312
Ruby Partial elapsed time 9.154

1. Starting Ruby tests...
Strings test - Elapsed 1.479
Arrays test - Elapsed 3.092
Numeric test - Elapsed 2.516
Ruby Partial elapsed time 7.087

2. Starting Ruby tests...
Strings test - Elapsed 1.431
Arrays test - Elapsed 3.056
Numeric test - Elapsed 2.508
Ruby Partial elapsed time 6.995

3. Starting Ruby tests...
Strings test - Elapsed 1.423
Arrays test - Elapsed 3.053
Numeric test - Elapsed 2.524
Ruby Partial elapsed time 7.000
-------------------------------------
Average Strings test - Elapsed 1.444
Average Arrays test - Elapsed 3.067
Average Numeric test - Elapsed 2.516

Ruby Average elapsed time 7.027

marco@d9400:~$ rvm use ruby-1.8.7-p249
marco@d9400:~$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
marco@d9400:~$ ruby RubyMM186_3.rb

Warming up...
Strings test - Elapsed 3.135
Check1: 50000
Check2: 157490
Check3: 50005
Check4: 373847
Arrays test - Elapsed 4.386
Check1: 20900
Check2: 250000
Check3: 250000
Check4: 1500000
Check5: 5749950
Numeric test - Elapsed 4.629
Check1: 6252500
Check2: 1439295494700374021157505910939096377494040420940312
Ruby Partial elapsed time 12.149

1. Starting Ruby tests...
Strings test - Elapsed 3.181
Arrays test - Elapsed 4.402
Numeric test - Elapsed 4.598
Ruby Partial elapsed time 12.180

2. Starting Ruby tests...
Strings test - Elapsed 3.153
Arrays test - Elapsed 4.387
Numeric test - Elapsed 4.572
Ruby Partial elapsed time 12.113

3. Starting Ruby tests...
Strings test - Elapsed 3.210
Arrays test - Elapsed 4.395
Numeric test - Elapsed 4.583
Ruby Partial elapsed time 12.189
-------------------------------------
Average Strings test - Elapsed 3.181
Average Arrays test - Elapsed 4.395
Average Numeric test - Elapsed 4.584

Ruby Average elapsed time 12.160

marco@d9400:~$ rvm use ruby-1.9.1
marco@d9400:~$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
marco@d9400:~$ ruby RubyMM191_3.rb

Warming up...
Strings test - Elapsed 2.452
Check1: 50000
Check2: 157490
Check3: 50005
Check4: 373847
Arrays test - Elapsed 4.488
Check1: 20900
Check2: 250000
Check3: 250000
Check4: 1500000
Check5: 5749950
Numeric test - Elapsed 1.840
Check1: 6252500
Check2: 1439295494700374021157505910939096377494040420940312
Ruby Partial elapsed time 8.780

1. Starting Ruby tests...
Strings test - Elapsed 2.314
Arrays test - Elapsed 4.679
Numeric test - Elapsed 1.843
Ruby Partial elapsed time 8.836

2. Starting Ruby tests...
Strings test - Elapsed 2.319
Arrays test - Elapsed 4.691
Numeric test - Elapsed 1.843
Ruby Partial elapsed time 8.853

3. Starting Ruby tests...
Strings test - Elapsed 2.318
Arrays test - Elapsed 4.705
Numeric test - Elapsed 1.847
Ruby Partial elapsed time 8.870
-------------------------------------
Average Strings test - Elapsed 2.317
Average Arrays test - Elapsed 4.692
Average Numeric test - Elapsed 1.844

Ruby Average elapsed time 8.853

marco@d9400:~$ rvm use ruby-1.9.2
marco@d9400:~$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
marco@d9400:~$ ruby RubyMM191_3.rb

Warming up...
Strings test - Elapsed 1.680
Check1: 50000
Check2: 157490
Check3: 50005
Check4: 373847
Arrays test - Elapsed 4.192
Check1: 20900
Check2: 250000
Check3: 250000
Check4: 1500000
Check5: 5749950
Numeric test - Elapsed 1.708
Check1: 6252500
Check2: 1439295494700374021157505910939096377494040420940312
Ruby Partial elapsed time 7.579

1. Starting Ruby tests...
Strings test - Elapsed 1.654
Arrays test - Elapsed 3.178
Numeric test - Elapsed 1.704
Ruby Partial elapsed time 6.536

2. Starting Ruby tests...
Strings test - Elapsed 1.746
Arrays test - Elapsed 3.388
Numeric test - Elapsed 1.699
Ruby Partial elapsed time 6.833

3. Starting Ruby tests...
Strings test - Elapsed 1.644
Arrays test - Elapsed 3.227
Numeric test - Elapsed 1.706
Ruby Partial elapsed time 6.577
-------------------------------------
Average Strings test - Elapsed 1.681
Average Arrays test - Elapsed 3.264
Average Numeric test - Elapsed 1.703

Ruby Average elapsed time 6.649

 
No Comments

Posted in JRuby, Ruby

 

Create skeleton applications with generators and templates

19 Mar

Last update on April 1th, 2010

Rails is a wonderful framework which help us to develop a modern web application. However, when you create a new one, you should perform several pretty boring operations: add plugins / gems, rescue a starting layout and whatever you usually use in yours applications.
To solve this, from version 2.3 you may use templates. You find lots of examples on internet, i give you another that show you how to take advantage of it combining with a cool rails generator. This guide explains how to make one. If you are impatient you can choose something already done, forking an existing project so did I. I customized Ryan Bates’s nifty-generators adding some new features:

  • nifty_layout now supports multiple styles: classic, and blackwhite cloudy. It are only a starting point
  • nifty_layout: I added the configuration file config.rb which is a container for all the application constants for example application name, the default style etc..
  • I introduced localization support: nifty_layout added YAML files for language en and it. To manage, some code is inserted into application controller: with the lang parameter you change the language setting (for example ?lang=en) that will be maintained throughout the session. The nifty_scaffold adds resources and its attributes, ready to be translated.
  • nifty_layout: the posts division has been moved into a partial in the shared folder. I preferred to do this to use it even through ajax
  • nifty_scaffold: the “new” and “edit” views now use two partial: render @ model which calls the second with the fields inside. So do the original version, it supports haml and sass, furthermore, I added support for formtastic. So you can choose the preferred combination!

The template I will show you download these new generators for you, however, these are the sources or you can even download latest gem and template.

What the generator should do and what the template?
Well, templates should be a container of things to do, executed only one time after creating a new project. Generators should create something like adding new files or customize something that already exists and can be executes whenever you want as well as within a template.

If you never have used a template before, you could start with a simple one or a tutorial could be even better:
this screencast show you how to make a template.
This is an article in html format.

I see many examples on internet which are filled with configuration’s parameters, that cause me strong headaches. I opted for one that ask me what i want. Surely, isn’t perfect yet but I think it’s a good base :

#This is a Rails 2.3 template
#Written by Marco Mastrodonato, last update on 01/04/2010 
#
# USAGE: rails yourapp -m template.rb
 
#This is the unique parameter, after first execution use FALSE to avoid installation
install_gems = false
git_http = true
 
WINDOWS = (RUBY_PLATFORM =~ /dos|win32|cygwin/i) || (RUBY_PLATFORM =~ /(:?mswin|mingw)/) 
 
#plugin/install doesn't work on my windows systems
def myplugin(name, url)
  url.sub! 'git://', 'http://' if git_http
  if WINDOWS
    #run "ruby script/plugin install #{url}"
    run "git clone #{url} vendor/plugins/#{name}"
  else
    plugin name, :git => url
  end
end
 
puts '*' * 40
puts "* Processing template#{WINDOWS ? ' on windows system': ''}..."
puts '*' * 40
 
use_git = yes?("Do you think to use git ?")
if use_git
  git :init
  file ".gitignore", <<-EOS.gsub(/^    /, '')
    .DS_Store
    log/*.log
    tmp/**/*
    config/database.yml
    db/*.sqlite3
    nbproject/*
  EOS
end
 
#Everyone must to have it!
plugin 'will_paginate',
        :git => "git://github.com/mislav/will_paginate.git" if yes?("Will Paginate ?")
 
# Attachments with no extra database tables, only one library to install for image processing
plugin 'paperclip',
       :git => "git://github.com/thoughtbot/paperclip.git" if yes?("Paperclip ?")
 
formtastic = yes?("Formtastic ?")
if formtastic
  #gem 'justinfrench-formtastic', :lib => 'formtastic', :source => 'http://gems.github.com'
  #rake "gems:install" if install_gem
 
  #A Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
  plugin 'formtastic',
        :git => "git://github.com/justinfrench/formtastic.git"
  #Adds reflective access to validations
  plugin 'validation_reflection',
        :git => "git://github.com/redinger/validation_reflection.git"
  generate "formtastic"
end
 
if yes?("Add testing framework ?")
  # RSpec's official Ruby on Rails plugin  
  plugin 'rspec', 
    :git => 'git://github.com/dchelimsky/rspec.git'
 
  plugin 'rspec-rails',
         :git => "git://github.com/dchelimsky/rspec-rails.git"
  generate "rspec"
 
  # BDD that talks to domain experts first and code 2nd
  plugin 'cucumber',
         :git => "git://github.com/aslakhellesoy/cucumber.git"
end
 
if yes?("Add authentication ?")
  puts "0. None"
  puts "1. Devise/warden"
  puts "2. Authlogic"
  puts "3. Restful authentication"
  choose = ask("Choose one:")
  case choose
    when "1"
      gem "warden", :version => "0.9.6"
      gem "devise", :version => "1.0.4"
      rake "gems:install" if install_gems
      generate "devise_install"
      generate "devise", "User"
      generate "devise_views"
      #User.create!(:email => 'admin@administrator.com', :password => 'secret')
    when "2"
      gem "authlogic"
      rake "gems:install" if install_gems
      generate "session", "user_session"
      generate "model", "user"
      generate "controller", "user_sessions"
      route "map.resource :user_session"
      create_users_file = Dir['db/migrate/*_create_users.rb'].first
      file create_users_file, <<-EOS.gsub(/^        /, '')
        class CreateUsers < ActiveRecord::Migration
          def self.up
            create_table :users do |t|
              t.string    :login,               :null => false                # optional, you can use email instead, or both
              t.string    :email,               :null => false                # optional, you can use login instead, or both
              t.string    :crypted_password,    :null => false                # optional, see below
              t.string    :password_salt,       :null => false                # optional, but highly recommended
              t.string    :persistence_token,   :null => false                # required
              t.string    :single_access_token, :null => false                # optional, see Authlogic::Session::Params
              t.string    :perishable_token,    :null => false                # optional, see Authlogic::Session::Perishability
 
              # Magic columns, just like ActiveRecord's created_at and updated_at. These are automatically maintained by Authlogic if they are present.
              t.integer   :login_count,         :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
              t.integer   :failed_login_count,  :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
              t.datetime  :last_request_at                                    # optional, see Authlogic::Session::MagicColumns
              t.datetime  :current_login_at                                   # optional, see Authlogic::Session::MagicColumns
              t.datetime  :last_login_at                                      # optional, see Authlogic::Session::MagicColumns
              t.string    :current_login_ip                                   # optional, see Authlogic::Session::MagicColumns
              t.string    :last_login_ip                                      # optional, see Authlogic::Session::MagicColumns
 
              t.timestamps
            end
 
            add_index :users, :email
            add_index :users, :login, :unique => true
          end
 
          def self.down
            drop_table :users
          end
        end
      EOS
      file 'app/models/user.rb', <<-EOS.gsub(/^        /, '')
        class User < ActiveRecord::Base
          acts_as_authentic
        end
      EOS
      file 'spec/models/user_spec.rb', <<-EOS.gsub(/^        /, '')
        require 'spec_helper'
 
        describe User do
          before(:each) do
            @valid_attributes = {
              :login                 => "login",
              :email                 => "some@thing.com",
              :password              => "password",
              :password_confirmation => "password"
            }
          end
 
          it "should create a new instance given valid attributes" do
            User.create!(@valid_attributes)
          end
        end
      EOS
    when "3"
      plugin 'restful-authentication-i18n',
          :git => "git://github.com/dcrec1/restful-authentication-i18n.git"
      #—include-activation \ —stateful \ —rspec \ —skip-migration \ —skip-routes \ —old-passwords 
      generate "authenticated", "user", "sessions", "—include-activation"
  end
 
  if yes?("Authorization ?")
    gem "cancan"
    rake "gems:install" if install_gems
    #plugin 'cancan', :git => "git://github.com/ryanb/cancan.git" 
    file 'spec/models/ability.rb', <<-EOS.gsub(/^      /, '')
      class Ability  
        include CanCan::Ability  
 
        def initialize(user)  
          user ||= User.new # Guest user  
          if user.role? :admin  
            can :manage, :all  
          else  
            can :read, :all  
          end  
        end  
      end  
    EOS
    generate "migration", "add_roles_mask_to_users roles_mask:integer"
    puts "Add these lines to user model:"
    puts <<-EOS.gsub(/^      /, '')
      named_scope :with_role, lambda { |role| {:conditions => "roles_mask & \#{2**ROLES.index(role.to_s)} > 0 "} }
      def roles=(roles)
        self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
      end
      def roles
        ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
      end
      def role_symbols
        roles.map(&:to_sym)
      end
    EOS
 
  end
 
end          
 
haml = yes?("Haml?")
if haml
  # The world's greatest templating system
  #plugin 'haml', :git => "git://github.com/nex3/haml.git"
  gem "haml", :lib => "haml"
  rake "gems:install" if install_gems
  run "haml --rails ."
end
 
if yes?("Layout ?")
  plugin "marcomd-nifty-generators", 
    :lib => "marcomd-nifty-generators",
    :git => "git://github.com/marcomd/nifty-generators.git"
  puts " 1. classic"
  puts " 2. cloudy"
  puts " 3. blackwhite"
  puts "or. write names separed by space"
  style = ask("Choose style:")
  style = case style
    when "1" then "classic"
    when "2" then "cloudy"
    when "3" then "blackwhite"
    else style
  end
  generate "nifty_layout", "application #{style} #{haml ? '--haml' : ''} #{formtastic ? '--formtastic' : ''}"
end
 
# Link to local copy of edge rails
#inside('vendor') { run 'ln -s ~/dev/rails/rails rails' } if yes?("Rails ?")
rake("rails:freeze:gems") if yes?("Freeze rails gems ?")
 
# Mailer dummy config
initializer "mailer.rb", <<-EOS.gsub(/^  /, '')
  mailer_options = YAML.load_file("\#{Rails.root}/config/mailer.yml")
  ActionMailer::Base.smtp_settings = mailer_options
EOS
file "config/mailer.yml", <<-EOS.gsub(/^  /, '')
  :address: mail.authsmtp.com
  :domain: yourdomain.com
  :authentication: :login
  :user_name: USERNAME
  :password: PASSWORD
EOS
 
if yes?("Generate controller home ? (suggested)")
  generate "controller", "home", "index"
  route "map.root :controller => :home"
else
 
end
 
rake "gems:install" if install_gems
# Unpack all gems to vendor/gems
rake "gems:unpack" if yes?("Unpack to vendor/gems ?")
rake "db:create:all"
rake "db:migrate"
File.unlink "public/index.html"
 
git :add => ".", :commit => "-m 'initial commit'" if use_git
 
puts "ENJOY!"

This is a result:


marco@d9400:~/Rails$ rails Blog -m marcomd.rb
create bla bla bla
applying template: marcomd.rb
Processing template...
plugin will_paginate
Unpacking objects: 100% (57/57), done.
From git://github.com/mislav/will_paginate
* branch HEAD -> FETCH_HEAD
Paperclip ?
y
plugin paperclip
Unpacking objects: 100% (78/78), done.
From git://github.com/thoughtbot/paperclip
* branch HEAD -> FETCH_HEAD
Formtastic ?
y
gem justinfrench-formtastic
generating formtastic_stylesheets
Add testing framework ?
y
plugin rspec
From git://github.com/dchelimsky/rspec
* branch HEAD -> FETCH_HEAD
plugin rspec-rails
From git://github.com/dchelimsky/rspec-rails
* branch HEAD -> FETCH_HEAD
plugin cucumber
From git://github.com/aslakhellesoy/cucumber
* branch HEAD -> FETCH_HEAD
Add authentication ?
y
0. None
1. Devise/warden
2. Authlogic
3. Restful authentication
Choose one:
1
gem warden
gem devise
generating devise_install
Authorization ?
n
Haml?
n
Layout ?
y
plugin marcomd-nifty-generators
From git://github.com/marcomd/nifty-generators
* branch HEAD -> FETCH_HEAD
1. classic
2. cloudy
3. blackwhite
or. write names separed by space
Choose style:
2
generating nifty_layout
Freeze rails gems ?
n
initializer mailer.rb
file config/mailer.yml
Generate controller home ? (suggested)
y
generating controller
route map.root :controller => :home
Unpack to vendor/gems ?
n
rake db:create:all
rake db:migrate
ENJOY!
applied marcomd.rb

And here we are! Now let’s start up mongrel…


marco@d9400:~/Rails$ cd Blog/
marco@d9400:~/Rails/Blog$ script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

anche check our new blog
This is only the beginning

Now, let’s add the post resource and migrate:


marco@d9400:~/Rails$ cd Blog/
marco@d9400:~/Rails/Blog$ script/generate nifty_scaffold post title:string body:text
exists app/models
create app/models/post.rb
exists db/migrate
create db/migrate/20100319175545_create_posts.rb
exists test/unit
create test/unit/post_test.rb
exists test/fixtures
create test/fixtures/posts.yml
exists app/controllers
create app/controllers/posts_controller.rb
exists app/helpers
create app/helpers/posts_helper.rb
create app/views/posts
create app/views/posts/index.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/edit.html.erb
create app/views/posts/_post.html.erb
create app/views/posts/_fields.html.erb
route map.resources :posts
exists test/functional
create test/functional/posts_controller_test.rb
marco@d9400:~/Rails/Blog$ rake db:migrate
(in /home/marco/Rails/Blog)
== CreatePosts: migrating ====================================================
-- create_table(:posts)
-> 0.0016s
== CreatePosts: migrated (0.0017s) ===========================================

Add post resource

Controllers and views uses I18n messages, for this reason, the new resource must be add to localization files. Nifty_scaffold will do it for you :

#config/locales/en.yml
en:
  activerecord:
    models: &models
      post: "Post"
      posts: "Posts"
    #DO NOT REMOVE MODELS
    attributes: &attributes
      title: "Title"
      body: "Body"
    #DO NOT REMOVE ATTRIBUTES
 
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: "Choose a good title for your article"
        body: "Choose a good body for your article"
    #DO NOT REMOVE HINTS
    actions: &actions
      create: "Create my {{model}}"
      update: "Save changes"
  <<: *models
  <<: *attributes
  <<: *actions
 
#config/locales/it.yml
it:
  activerecord:
    models: &models
      post: "Messaggio"
      posts: "Messaggi"
    #DO NOT REMOVE MODELS
    attributes: &attributes
      title: "Titolo"
      body: "Corpo"
    #DO NOT REMOVE ATTRIBUTES
 
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: "Choose a good title for your article"
        body: "Choose a good body for your article"
    #DO NOT REMOVE HINTS
    actions: &actions
      create: "Crea {{model}}"
      update: "Salva le modifiche"
  #DO NOT REMOVE FORMTASTIC
  <<: *models
  <<: *attributes
  <<: *actions

I have to use tag #DO NOT REMOVE ecc. to put attributes and resource name inside so please don’t remove it.

Post created sucessfully

I recently began testing Haml and Sass version for which there may be still some bugs.
I hope this can be helpful, enjoy!

 
 

Ruby vs Python vs Windows vs Linux

06 Mar

In the previous article, I examined the performance of some ruby and python interpreters on a Windows XP system. This time, I run the same script under Linux distribution: Ubuntu 9.10 2.6.31-20-generic. APT versions were installed via package manager, the RVM instead, through the Ruby Version Manager which build from sources.

Version Builder/System/VM Seconds
JRuby 1.4.0 APT Java HotSpot(TM) 64-Bit Server VM 1.6.0_16) [amd64-java] 6,1
JRuby 1.4.0 RVM Java HotSpot(TM) 64-Bit Server VM 1.6.0_16) [amd64-java] 6,2
Ruby 1.9.2 preview1 RVM x86_64-linux 6,5
JRuby 1.4.0 Windows Client VM 1.6.0_15 [x86-java] 6,9
Python 2.6.4 Windows 7,5
Ruby 1.9.1 p129 i386-mingw32 8,1
Python 2.6.4 APT x86_64-linux 8,7
Ruby 1.9.1 p378 RVM x86_64-linux 8,7
Ruby 1.9.1 p243 RVM x86_64-linux 8,8
Python 3.1.1 Windows 9,0
Ruby 1.9.1 p243 APT x86_64-linux 9,3
Ruby 1.9.1 p243 i386-mingw32 9,6
Ruby 1.8.7 p249 RVM x86_64-linux 12,2
IronRuby 0.9.3.0 Windows .NET 2.0.0.0 18,9
Ruby 1.9.1 p376 i386-mswin32 20,8
Ruby 1.8.7 p174 APT x86_64-linux 23,0
Ruby 1.8.6 p368 i386-mingw32 23,3
Ruby 1.8.7 p249 i386-mingw32 23,9
IronPython 2.6 Windows .NET 2.0.0.0 256,5
Jython 2.5.1 Windows Client VM 1.6.0_15 [x86-java] Timeout

Win vs Linux

Win vs Linux

 

Ruby vs Ruby vs Python vs Python

24 Jan

Another benchmark to compare some recent versions of ruby with the latest releases of Python. Two simple scripts to compare the syntax and performance of these modern languages.
The system on which I performed the test is a Dell Inspiron 9400 with Centrino Duo, Intel T7200 4Mb Cache, 2GHz, 2GB RAM 667Mhz. Windows XP Pro SP3.

This is the result of the first test, used to warming up for the VM:

Version Builder Seconds
Python 2.6.4 7,5
Ruby 1.9.1 p129 i386-mingw32 8,2
JRuby 1.4.0 Client VM 1.6.0_15 [x86-java] 9,0
Python 3.1.1 9,1
Ruby 1.9.1 p243 i386-mingw32 9,6
IronRuby 0.9.3.0 .NET 2.0.0.0 20,3
Ruby 1.9.1 p376 i386-mswin32 20,9
Ruby 1.8.6 p368 i386-mingw32 22,9
IronPython 2.6 .NET 2.0.0.0 225,4
Jython 2.5.1 Client VM 1.6.0_15 [x86-java] Timeout

Benchmark without warm up

Benchmark without warm up

The result below instead, refers to the average of three readings after the warm up. JRuby’s performance improves by 23%:

Version Builder Seconds
JRuby 1.4.0 Client VM 1.6.0_15 [x86-java] 6,9
Python 2.6.4 7,5
Ruby 1.9.1 p129 i386-mingw32 8,2
Python 3.1.1 9,0
Ruby 1.9.1 p243 i386-mingw32 10,0
IronRuby 0.9.3.0 .NET 2.0.0.0 18,9
Ruby 1.9.1 p376 i386-mswin32 20,6
Ruby 1.8.6 p368 i386-mingw32 23,2
IronPython 2.6 .NET 2.0.0.0 256,5
Jython 2.5.1 Client VM 1.6.0_15 [x86-java] Timeout

Benchmark after warm up

Benchmark after warm up

And here is the scripts. I tried to optimize every versions and to do that, I had to create two variations for each language.

Ruby 1.8.6:

def strings_test(ntest)
  r1 = r2 = r3 = 0
  xstr = ""
  ntest.times do 
    #Create a string, add 'abcde1234_' until getting a str size 1000
    xstr = 'abcde1234_' * 10000
 
    #Make letters upcase 
    xstr.upcase!
 
    #Change '1234_' with '67890 ' (space at last position)
    #Now the repeated string should be 'ABCDE67890 '
    xstr.gsub! '1234_', '67890 '
 
    #Cast numbers from 29 upto size/2 to string and add it to xstr variable, ciclying for every number (not add all numbers one time)
    29.upto(xstr.size/2) {|n| xstr << n.to_s}
 
    #Check 1: Count 'A' char 
    #Check 2: Count '9' char 
    0.upto(xstr.size-1) do |n| 
      if xstr[n].chr() == 'A'
        r1+=1 
      elsif xstr[n].chr() == '9'
        r2+=1
      end
    end
 
    #Create an array from xstr using space to split
    r3 += xstr.split.size
 
  end
 
  return r1, r2, r3, xstr
end
 
def arrays_test(ntest, xstr)
  r1 = r2 = r3 = r4 = r5 = 0
  ntest.times do 
    #Clear ar then add 5000 times this element: "I", "am", "great", null, "or", "number", 1
    ar =  []
    5000.times do
      ['I', 'am', 'great', nil, 'or', 'number', 1].each {|a| ar << a}
    end
 
    #...then reverse elements to obtain this order: 1, "number", "or", null, "great", "am", "I"
    ar.reverse!
 
    #...then, count the element with value "great" using two separate cicle
    #the first starting from 31 until 2955 (bounty inclused)
    31.upto(2955) do |n|
      r1 += 1 if ar[n] == 'great'
    end
    #the second looping all the array elements
    ar.each {|n| r2+=1 if n == 'great'}
 
    #Loop inside and build a temporary string with index and value, without put it into a variable and only for elements <> null
    ar.each_index{|i| ar[i] ? "#{i} #{ar[i]}" : r3+=1 }
 
    #delete null value elements and take its size
    ar.compact!
    r4 += ar.size
 
    #then join elements with space and take its size
    r5 += ar.join(' ').size
 
  end
 
  return r1, r2, r3, r4, r5
end
 
def nums_test(ntest)
  r1 = r2 = 0
 
  ntest.times do 
    #Find all prime numbers from 8 to 95 step by 3 and sum all primes got, to check the result
    #51.upto(307) do |n| 
    (8..95).step(3) do |n|
      primes(n).each {|a| r1 += a}
    end
 
    #Calculate factorial numbers start from 2 to 42
    r2 = 0
    for n in 2..42
      r2 += fac(n)
    end
  end
 
  return r1, r2
end
 
#Primes must return an array of prime numbers
def primes(n)
  ar = []
  for x in (2..n)
    prime = true
    for y in (2..x-1)
      if x%y == 0
        prime = false
        break
      end
    end
    ar << x if prime
  end
  return ar
end
 
def fac(n) (1..n).inject{|total, current| total * current} end
 
# ---  START  ---
puts "\nWarming up..."
t1=t2=t3=0
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
puts "Check3: #{r3}"
puts "Check4: #{xstr.size}"
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
puts "Check3: #{r3}"
puts "Check4: #{r4}"
puts "Check5: #{r5}"
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
 
puts "\n1. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "\n2. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "\n3. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "-------------------------------------"
puts "Average Strings test - Elapsed %.3f" % (t1/3)
puts "Average Arrays test  - Elapsed %.3f" % (t2/3)
puts "Average Numeric test - Elapsed %.3f" % (t3/3)
puts "\nRuby Average elapsed time %.3f" % (t1/3+t2/3+t3/3)

Ruby 1.9.1:

def strings_test(ntest)
  r1 = r2 = r3 = 0
  xstr = ""
  ntest.times do 
    #Create a string, add 'abcde1234_' until getting a str size 1000
    xstr = 'abcde1234_' * 10000
 
    #Make letters upcase 
    xstr.upcase!
 
    #Change '1234_' with '67890 ' (space at last position)
    #Now the repeated string should be 'ABCDE67890 '
    xstr.gsub! '1234_', '67890 '
 
    #Cast numbers to string, from 29 up to size/2. Add it to xstr variable as well, ciclying for every number (not adding all numbers once)
    29.upto(xstr.size/2) {|n| xstr << n.to_s}
 
    #Check 1: Count 'A' char 
    #Check 2: Count '9' char 
    0.upto(xstr.size-1) do |n| 
      if xstr[n] == 'A'
        r1+=1 
      elsif xstr[n] == '9'
        r2+=1
      end
    end
 
    #Create an array from xstr using space to split, its size is the third check
    r3 += xstr.split.size
 
  end
 
  return r1, r2, r3, xstr
end
 
def arrays_test(ntest, xstr)
  r1 = r2 = r3 = r4 = r5 = 0
  ntest.times do 
    #Clear ar then add 5000 times this element: "I", "am", "great", null, "or", "number", 1
    ar =  []
    5000.times do
      ['I', 'am', 'great', nil, 'or', 'number', 1].each {|a| ar << a}
    end
 
    #...then reverse elements to obtain this order: 1, "number", "or", null, "great", "am", "I"
    ar.reverse!
 
    #...then, count the element with value "great" using two separate cicle
    #the first starting from 31 until 2955 (bounty inclused)
    31.upto(2955) do |n|
      r1 += 1 if ar[n] == 'great'
    end
    #the second looping all the array elements
    ar.each {|n| r2+=1 if n == 'great'}
 
    #Loop inside and build a temporary string with index and value, without put it into a variable and only for elements <> null
    ar.each_index{|i| ar[i] ? "#{i} #{ar[i]}" : r3+=1 }
 
    #delete null value elements and take its size
    ar.compact!
    r4 += ar.size
 
    #then join elements with space and take its size
    r5 += ar.join(' ').size
 
  end
 
  return r1, r2, r3, r4, r5
end
 
def nums_test(ntest)
  r1 = r2 = 0
 
  ntest.times do 
    #Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result
    #51.upto(307) do |n| 
    (8..95).step(3) do |n|
      primes(n).each {|a| r1 += a}
    end
 
    #Calculate factorial numbers start from 2 to 42
    r2 = 0
    for n in 2..42
      r2 += fac(n)
    end
  end
 
  return r1, r2
end
 
#Primes must return an array of prime numbers
def primes(n)
  ar = []
  for x in (2..n)
    prime = true
    for y in (2..x-1)
      if x%y == 0
        prime = false
        break
      end
    end
    ar << x if prime
  end
  return ar
end
 
def fac(n) (1..n).inject{|total, current| total * current} end
 
# ---  START  ---
puts "\nWarming up..."
t1=t2=t3=0
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
puts "Check3: #{r3}"
puts "Check4: #{xstr.size}"
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
puts "Check3: #{r3}"
puts "Check4: #{r4}"
puts "Check5: #{r5}"
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
puts "Check1: #{r1}"
puts "Check2: #{r2}"
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
 
puts "\n1. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "\n2. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "\n3. Starting Ruby tests..."
 
time = Time.now
r1, r2, r3, xstr = strings_test(5)
puts "Strings test - Elapsed %.3f" % (p1=Time.now - time)
 
time = Time.now
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
puts "Arrays test  - Elapsed %.3f" % (p2=Time.now - time)
 
time = Time.now
r1, r2 = nums_test(500)
puts "Numeric test - Elapsed %.3f" % (p3=Time.now - time)
 
puts "Ruby Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
puts "-------------------------------------"
puts "Average Strings test - Elapsed %.3f" % (t1/3)
puts "Average Arrays test  - Elapsed %.3f" % (t2/3)
puts "Average Numeric test - Elapsed %.3f" % (t3/3)
puts "\nRuby Average elapsed time %.3f" % (t1/3+t2/3+t3/3)

Python 2.6:

from time import time
#import psyco
#psyco.full()
#psyco.full(memory=100)
#psyco.profile(0.05, memory=100)
#psyco.profile(0.2)
 
 
def strings_test(ntest):
  r1 = r2 = r3 = 0
  xstr = ""
  for x in xrange(ntest):
    #Create a string, add 'abcde1234_' until getting a xstr size 1000
    xstr = 'abcde1234_' * 10000
 
    #Make letters upcase 
    xstr = xstr.upper()
 
    #Change '1234_' with '67890 ' (space at last position)
    #Now the repeated string should be 'ABCDE67890 '
    xstr = xstr.replace('1234_', '67890 ')
 
    #Cast numbers to string, from 29 up to size/2. Add it to xstr variable as well, ciclying for every number (not adding all numbers once)
    for y in xrange(29,int(len(xstr)/2) + 1):
        xstr += "%s" %y
 
    #Result 1: Count 'A' char 
    #Result 2: Count '9' char 
    for y in xrange(0, len(xstr)):
      if xstr[y] == 'A':
        r1+=1 
      elif xstr[y] == '9':
        r2+=1
 
    #Create an array from xstr using space to split
    r3 += len(xstr.split())
 
 
  return r1, r2, r3, xstr
 
#Slower than other version
def multiremove(ar, what):
  i = 0
  for el in ar:
    if el == what:
      del ar[i]
    i+=1
 
#Ugly but a bit faster
def multiremove2(ar, what):
  todel = [] 
  for y in xrange(0,len(ar)):
    if ar[y] == what:
       todel.append(y)
  todel.reverse()
  for y in todel:
     ar.pop(y)
 
def arrays_test(ntest, xstr):
  r1 = r2 = r3 = r4 = r5 = 0
  for x in xrange(ntest):
    #Clear ar then add 5000 times this element: "I", "am", "great", null, "or", "number", 1
    ar = []
    for y in xrange(0, 5000):
      ar.extend(["I", "am", "great", None, "or", "number", 1])
 
    #...then reverse elements to obtain this order: 1, "number", "or", null, "great", "am", "I"
    ar.reverse()
 
    #...then, count the element with value "great" using two separate cicle
    #the first starting from 31 until 2955 (bounty included)
    for y in xrange(31,2955):
      if ar[y] == "great": r1 +=1
 
    #the second looping all the array elements
    for y in xrange(0, len(ar)):
      if ar[y] == "great": r2+=1
 
    #Loop inside and build a temporary string with index and value, without put it into a variable and only for elements <> null
    for y in xrange(0, len(ar)):
      if ar[y]:
        "%s %s" %(y, ar[y])
      else:
        r3+=1
 
    #delete null value elements and take its size
    multiremove2(ar, None)
    r4 += len(ar)
 
    #then join elements with space and take its size
    r5 += len(" ".join(str(n) for n in ar))
 
  return r1, r2, r3, r4, r5
 
def nums_test(ntest):
  r1 = r2 = 0
  for x in xrange(ntest):
    #Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result
    for n in xrange(8, 96, 3):
      for prime in primes(n):
        r1 += prime
    fac = lambda n:[1,0][n>0] or fac(n-1)*n
    #Calculate factorial numbers start from 2 to 42 (bounds included)
    r2 = 0
    for n in xrange(2, 43):
      r2 += fac(n)
 
  return r1, r2
 
#Primes must return an array of prime numbers
def primes(n):
  ar = []
  for x in xrange(2, n+1):
    prime = True
    for y in xrange(2, x):
      if x%y == 0:
        prime = False
        break
    if prime:
      ar.append(x)
  return ar
 
# ---  START  ---
print "\nWarming up..."
t1=t2=t3=0
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print "Strings test - Elapsed %.3f" % (p1)
print "Check1: %s" %r1
print "Check2: %s" %r2
print "Check3: %s" %r3
print "Check4: %d" %(len(xstr))
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print  "Arrays test - Elapsed %.3f" % (p2)
print "Check1: %s" %r1
print "Check2: %s" %r2
print "Check3: %s" %r3
print "Check4: %s" %r4
print "Check5: %s" %r5
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print "Numeric test  - Elapsed %.3f" % (p3)
print "Check1: %s" %r1
print "Check2: %s" %r2
 
print  "Python Partial elapsed time %.3f" % (p1+p2+p3)
 
print "\n1. Starting Python tests..."
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print "Strings test - Elapsed %.3f" % (p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print  "Arrays test  - Elapsed %.3f" % (p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print "Numeric test - Elapsed %.3f" % (p3)
 
print  "Python Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
print "\n2. Starting Python tests..."
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print "Strings test - Elapsed %.3f" % (p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print  "Arrays test  - Elapsed %.3f" % (p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print "Numeric test - Elapsed %.3f" % (p3)
 
print  "Python Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
print "\n3. Starting Python tests..."
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print "Strings test - Elapsed %.3f" % (p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print  "Arrays test  - Elapsed %.3f" % (p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print "Numeric test - Elapsed %.3f" % (p3)
 
print  "Python Partial elapsed time %.3f" % (p1+p2+p3)
t1+=p1;t2+=p2;t3+=p3
 
print "-------------------------------------"
print "Average Strings test - Elapsed %.3f" % (t1/3)
print "Average Arrays test  - Elapsed %.3f" % (t2/3)
print "Average Numeric test - Elapsed %.3f" % (t3/3)
print "Python Average elapsed time %.3f" % (t1/3+t2/3+t3/3)

Python 3.1:

from time import time
#import psyco
#psyco.full()
#psyco.full(memory=100)
#psyco.profile(0.05, memory=100)
#psyco.profile(0.2)
 
 
def strings_test(ntest):
  r1 = r2 = r3 = 0
  xstr = ""
  for x in range(ntest):
    #Create a string, add 'abcde1234_' until getting a xstr size 1000
    xstr = 'abcde1234_' * 10000
 
    #Make letters upcase 
    xstr = xstr.upper()
 
    #Change '1234_' with '67890 ' (space at last position)
    #Now the repeated string should be 'ABCDE67890 '
    xstr = xstr.replace('1234_', '67890 ')
 
    #Cast numbers from 29 upto 1028 to string and add it to xstr variable, ciclying for every number (not add all numbers one time)
    for y in range(29,int(len(xstr)/2) + 1):
        xstr += "%s" %y
 
    #Result 1: Count 'A' char 
    #Result 2: Count '9' char 
    for y in range(0, len(xstr)):
      if xstr[y] == 'A':
        r1+=1 
      elif xstr[y] == '9':
        r2+=1
 
    #Create an array from xstr using space to split
    r3 += len(xstr.split())
 
 
  return r1, r2, r3, xstr
 
#Slower than other version
def multiremove(ar, what):
  i = 0
  for el in ar:
    if el == what:
      del ar[i]
    i+=1
 
#Ugly but a bit faster
def multiremove2(ar, what):
  todel = [] 
  for y in range(0,len(ar)):
    if ar[y] == what:
       todel.append(y)
  todel.reverse()
  for y in todel:
     ar.pop(y)
 
def arrays_test(ntest, xstr):
  r1 = r2 = r3 = r4 = r5 = 0
  for x in range(ntest):
    #Clear ar then add 5000 times this element: "I", "am", "great", null, "or", "number", 1
    ar = []
    for y in range(0, 5000):
      ar.extend(["I", "am", "great", None, "or", "number", 1])
 
    #...then reverse elements to obtain this order: 1, "number", "or", null, "great", "am", "I"
    ar.reverse()
 
    #...then, count the element with value "great" using two separate cicle
    #the first starting from 31 until 2955 (bounty included)
    for y in range(31,2955):
      if ar[y] == "great": r1 +=1
 
    #the second looping all the array elements
    for y in range(0, len(ar)):
      if ar[y] == "great": r2+=1
 
    #Loop inside and build a temporary string with index and value, without put it into a variable and only for elements <> null
    for y in range(0, len(ar)):
      if ar[y]:
        "%s %s" %(y, ar[y])
      else:
        r3+=1
 
    #delete null value elements and take its size
    multiremove2(ar, None)
    r4 += len(ar)
 
    #then join elements with space and take its size
    r5 += len(" ".join(str(n) for n in ar))
 
  return r1, r2, r3, r4, r5
 
def nums_test(ntest):
  r1 = r2 = 0
  for x in range(ntest):
    #Find all prime numbers from 8 to 95 step by 3 (bounds included) and sum all primes got, to check the result
    for n in range(8, 96, 3):
      for prime in primes(n):
        r1 += prime
    fac = lambda n:[1,0][n>0] or fac(n-1)*n
    #Calculate factorial numbers start from 2 to 42 (bounds included)
    r2 = 0
    for n in range(2, 43):
      r2 += fac(n)
 
  return r1, r2
 
#Primes must return an array of prime numbers
def primes(n):
  ar = []
  for x in range(2, n+1):
    prime = True
    for y in range(2, x):
      if x%y == 0:
        prime = False
        break
    if prime:
      ar.append(x)
  return ar
 
# ---  START  ---
print("\nWarming up...")
t1=t2=t3=0
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print("Strings test - Elapsed %.3f" % p1)
print("Check1: %s" % r1)
print("Check2: %s" % r2)
print("Check3: %s" % r3)
print("Check4: %d" % len(xstr))
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print("Arrays test - Elapsed %.3f" % p2)
print("Check1: %s" % r1)
print("Check2: %s" % r2)
print("Check3: %s" % r3)
print("Check4: %s" % r4)
print("Check5: %s" % r5)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print("Numeric test - Elapsed %.3f" % p3)
print("Check1: %s" % r1)
print("Check2: %s" % r2)
 
print( "Python Partial elapsed time %.3f" % (p1+p2+p3))
 
print("\n1. Starting Python tests...")
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print("Strings test - Elapsed %.3f" % p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print( "Arrays test  - Elapsed %.3f" % p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print("Numeric test - Elapsed %.3f" % p3)
 
print( "Python Partial elapsed time %.3f" % (p1+p2+p3))
t1+=p1;t2+=p2;t3+=p3
 
print("\n2. Starting Python tests...")
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print("Strings test - Elapsed %.3f" % p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print( "Arrays test  - Elapsed %.3f" % p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print("Numeric test - Elapsed %.3f" % p3)
 
print( "Python Partial elapsed time %.3f" % (p1+p2+p3))
t1+=p1;t2+=p2;t3+=p3
 
print("\n3. Starting Python tests...")
 
stime = time()
r1, r2, r3, xstr = strings_test(5)
p1=time() - stime
print("Strings test - Elapsed %.3f" % p1)
 
stime = time()
r1, r2, r3, r4, r5 = arrays_test(50, xstr)
p2=time() - stime
print( "Arrays test  - Elapsed %.3f" % p2)
 
stime = time()
r1, r2 = nums_test(500)
p3=time() - stime
print("Numeric test - Elapsed %.3f" % p3)
 
print( "Python Partial elapsed time %.3f" % (p1+p2+p3))
t1+=p1;t2+=p2;t3+=p3
 
print("-------------------------------------")
print("Average Strings test - Elapsed %.3f" % (t1/3))
print("Average Arrays test  - Elapsed %.3f" % (t2/3))
print("Average Numeric test - Elapsed %.3f" % (t3/3))
print("Python Average elapsed time %.3f" % (t1/3+t2/3+t3/3))
 

Manage routing errors

20 Nov

Our application can only respond to certain requests, of course, to those that we have anticipated. For all other generates an error depending on the type:

  • Routing errors
  • Errors inside the controller

Routing errors

Small premise: In a traditional application, to retrieve a nonexistent page from the web server, we received a code 404. In a framework with a pattern mvc instead, requests through controllers and this allows us to define the behavior. The requests are performed according to the routing rules and in Rails for convention, all the controllers can be addressed. If we open the file routes.rb, at the bottom we discover why:

#configroutes.rb
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

To ensure that only certain controllers can be routed, just delete those two lines at the bottom of the file that only defines the form of the request, leaving only the explicit routes. End of the preface.

If there isn’t a route, this generates a routing error because Rails does not know what we want. To handle this event, simply add one last road before you generate the error, which will inform them that has not found other valid roads:

#configroutes.rb
 
#at the bottom
map.catch_all "*anything" , :controller => "home" , :action => "unknown_request"

We saw how catch the not handled routes and directs it to the controller where we want to manage it, for that example at home controller using the method unknown_request:

#appcontrollershome_controller.rb
def unknown_request
  respond_to do |format|
    format.html do
      flash[:error] = I18n.t(:unknown_request)
      redirect_to root_path
    end
    format.xml  {  render :xml => {:root => I18n.t(:unknown_request) }, :status => :unprocessable_entity }
  end
end

Errors inside the controller

If the controller has been addressed, does not mean that the request is correct, because each has a series of operations that must exist and must be able to operate on the resource and manage the exceptions raised by the database.

Since version 2.0 of Rails, we can use “rescue_from” in the controller:

#appcontrollersmy_controller.rb
 
class MyController < ApplicationController
  rescue_from ActionController::UnknownAction, :with => :action_not_found
  rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
 
  def action_not_found
    render 'shared/action_not_found', :layout => true, :status => 404
  end
  def record_not_found
    render 'shared/record_not_found', :layout => true, :status => 404
  end
end

Here are the views:

#appviewssharedaction _not_found.html.erb
<h2>Sorry, the action doesn't exists!</h2>
 
#appviewssharedrecord_not_found.html.erb
<h2>Sorry, the record doesn't exists!</h2>

Another more concise way is to pass a proc as a parameter to “rescue_from”:

#appcontrollersmy_controller.rb
rescue_from(ActiveRecord::RecordNotFound) { |e| render 'shared/record_not_found', :layout => true, :status => 404 }
 
Comments Off

Posted in Ruby on Rails

 

Comparison script languages for the fractal geometry

24 Aug

This article will compare the latest incarnations of Ruby, with the latest in Python, Groovy, PHP, Lua, Perl and Java too, to have a comparison with a pre-compiled language. We will see, how scripting languages behave if applied to fractal geometry, more precisely an family Mandelbrot algorithm.
Browsing on the net, I found a comparison very interesting but a bit dated, dates back more than two years ago. Since then things have changed and I took advantage to make an update, not including all of those languages but only for more known. This is an opportunity to compare Ruby and Python versions even in their Java and. NET, an intention that I had since long time.

Using a fractal as a convenient benchmark plus: if an attempt to optimize not be successful if it has been the evidence and being drawn in real time, you can feel the speed. The fractal is drawn in ASCII also because the use of external libraries would have drugged the outcome.

                                       *
                                       *
                                       *
                                       *
                                       *
                                      ***
                                     *****
                                     *****
                                      ***
                                       *
                                   *********
                                 *************
                                ***************
                             *********************
                             *********************
                              *******************
                              *******************
                              *******************
                              *******************
                            ***********************
                              *******************
                              *******************
                             *********************
                              *******************
                              *******************
                               *****************
                                ***************
                                 *************
                                   *********
                                       *
                                ***************
                            ***********************
                         * ************************* *
                         *****************************
                      * ******************************* *
                       *********************************
                      ***********************************
                    ***************************************
               *** ***************************************** ***
               *************************************************
                ***********************************************
                 *********************************************
                 *********************************************
                ***********************************************
                ***********************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
              ***************************************************
         *    ***************************************************    *
       *****  ***************************************************  *****
       ****** *************************************************** ******
      ******* *************************************************** *******
    ***********************************************************************
    ********* *************************************************** *********
       ****** *************************************************** ******
       *****  ***************************************************  *****
              ***************************************************
              ***************************************************
              ***************************************************
              ***************************************************
               *************************************************
               *************************************************
              ***************************************************
                ***********************************************
                ***********************************************
                  *******************************************
                   *****************************************
                 *********************************************
                **** ****************** ****************** ****
                 ***  ****************   ****************  ***
                  *    **************     **************    *
                         ***********       ***********
                         **  *****           *****  **
                          *   *                 *   *

This is the system for the test:
Dell Inspiron 9400, Centrino Duo, T7200 @ 2Ghz 4Mb Cache L1, Ram 2Gb @ 667Mhz
Windows XP pro SP3
Java 6 update 15
Microsoft .NET 3.5 SP1

These are the performance results obtained from an average of five runs, took after have executed some void attempts (I have not trusted the VM startup):

Language      Time (in seconds)  n times slower tha java
_____________________________________________________________
Java 6 update 15    0,153
Lua 5.1.4           0,815	           5x
Php 5.3.0           2,083	          14x
Python 2.6.2        2,269 	          15x
Python 3.1.1        1,566 	          10x
Jython 2.5.0        2,850 	          19x
Jruby 1.3.1         2,466 	          16x
Groovy 1.6.3        6,491 	          42x
Ruby 1.9.1 p129	    2,688 	          18x
Ruby 1.8.6 p368	    6,863 	          45x
Ruby 1.8.6 p111	    9,709 	          63x
IronRuby 0.9.0	    6,038 	          39x
IronPyhon 2.0.2     0,978 	           6x
Perl 5.10.0         2,722 	          18x

This is the chart, of course lower values indicate better performance

Chart

These are the scripts used to generate the fractal, the originals was good, I have only done a few simple changes to run Python 3.1 or slightly improve the already excellent readability in Ruby and Lua.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// by Erik Wrenholt
import java.util.*;
 
class Bench1
{  
	static int BAILOUT = 16;
	static int MAX_ITERATIONS = 1000;
 
	private static int iterate(float x, float y)
	{
		float cr = y-0.5f;
		float ci = x;
		float zi = 0.0f;
		float zr = 0.0f;
		int i = 0;
		while (true) {
			i++;
			float temp = zr * zi;
			float zr2 = zr * zr;
			float zi2 = zi * zi;
			zr = zr2 - zi2 + cr;
			zi = temp + temp + ci;
			if (zi2 + zr2 > BAILOUT)
				return i;
			if (i > MAX_ITERATIONS)
				return 0;
		}
	}
 
	public static void main(String args[])
	{
		Date d1 = new Date();
		int x,y;
		for (y = -39; y < 39; y++) {
			System.out.print("n");
			for (x = -39; x < 39; x++) {
				if (iterate(x/40.0f,y/40.0f) == 0) 
					System.out.print("*");
				else
					System.out.print(" ");
 
			}
		}
		Date d2 = new Date();
		long diff = d2.getTime() - d1.getTime();
		System.out.println("nJava Elapsed " + diff/1000.0f);
 
	}
}

Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- By Erik Wrenholt
 
local BAILOUT = 16
local MAX_ITERATIONS = 1000
 
function iterate(x,y)
 
  local cr = y-0.5
  local ci = x
  local zi = 0.0
  local zr = 0.0
  local i = 0
 
  while 1 do
    i = i+1
    local temp = zr * zi
    local zr2 = zr*zr
    local zi2 = zi*zi
    zr = zr2-zi2+cr
    zi = temp+temp+ci
    if (zi2+zr2 > BAILOUT) then
      return i
    end
    if (i > MAX_ITERATIONS) then
      return 0
    end
  end
end
 
function bench1()
  local t = os.clock()
  for y = -39, 38 do
    for x = -39, 38 do
    if (iterate(x/40.0, y/40) == 0) then io.write("*") else io.write(" ") end
    end
    io.write("n")
  end
  io.write(string.format("Time Elapsed %.3fn", os.clock() - t))
end
 
bench1()

Php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
define("BAILOUT",16);
define("MAX_ITERATIONS",1000);
 
class Bench1
{
 
	function Bench1()
	{
		$d1 = microtime(1);
		for ($y = -39; $y < 39; $y++) {
			echo("n");
			for ($x = -39; $x < 39; $x++) {
				if ($this->iterate($x/40.0,$y/40.0) == 0) 
					echo("*");
				else
					echo(" ");
			}
		}
		$d2 = microtime(1);
		$diff = $d2 - $d1;
		printf("nPHP Elapsed %0.3f", $diff);
	}
 
	function iterate($x,$y)
	{
		$cr = $y-0.5;
		$ci = $x;
		$zi = 0.0;
		$zr = 0.0;
		$i = 0;
		while (true) {
			$i++;
			$temp = $zr * $zi;
			$zr2 = $zr * $zr;
			$zi2 = $zi * $zi;
			$zr = $zr2 - $zi2 + $cr;
			$zi = $temp + $temp + $ci;
			if ($zi2 + $zr2 > BAILOUT)
				return $i;
			if ($i > MAX_ITERATIONS)
				return 0;
		}
	}
}
 
new Bench1();
?>

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys, time
stdout = sys.stdout
 
BAILOUT = 16
MAX_ITERATIONS = 1000
 
class Bench1:
  def __init__(self):
    print ('Rendering...')
    for y in range(-39, 39):
      stdout.write('n')
      for x in range(-39, 39):
        i = self.start(x/40.0, y/40.0)
 
        if i == 0:
          stdout.write('*')
        else:
          stdout.write(' ')
 
  def start(self, x, y):
    cr = y - 0.5
    ci = x
    zi = zr = 0.0
    i = 0
 
    while True:
      i += 1
      temp = zr * zi
      zr2 = zr * zr
      zi2 = zi * zi
      zr = zr2 - zi2 + cr
      zi = temp + temp + ci
 
      if zi2 + zr2 > BAILOUT:
        return i
      if i > MAX_ITERATIONS:
        return 0
 
t = time.time()
Bench1()
print ('nPython Elapsed %.3f' % (time.time() - t))

Groovy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Created By Marco Mastrodonato 22/09/2009
 
class Bench1{
    public int BAILOUT = 16
    public int MAX_ITERATIONS = 1000
 
    def Bench1(){
        println("Rendering...")
        for (y in -39..39){
            println("")
            for (x in -39..39){
                if (iterate(x/40.0, y/40.0) == 0){
                    print("*")
                } else {
                    print(" ")
                }
            }
        }
    }
 
    def iterate(x,y){
        float cr = y-0.5
        float ci = x
        float zi = 0.0
        float zr = 0.0
        def i = 0
        while(1){
            i += 1
            float temp = zr * zi
            float zr2 = zr * zr
            float zi2 = zi * zi
            zr = zr2 - zi2 + cr
            zi = temp + temp + ci
            if (zi2 + zr2 > BAILOUT){ 
                return i
            }
            if (i > MAX_ITERATIONS){ 
                return 0
            } 
        }
    }
 
}
 
time1 = new Date().time
new Bench1()
time2 = new Date().time
float elapsed = (time2 - time1)/1000
println("nGroovy Elapsed ${elapsed}")

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
BAILOUT = 16
MAX_ITERATIONS = 1000
 
class Bench1
 
  def initialize
    puts "Rendering..."
    for y in -39..39
      print "n"
      for x in -39..39
        i = iterate x/40.0, y/40.0
        if i == 0 then print "*" else print " " end
      end
    end
  end
 
  def iterate(x,y)
    cr = y-0.5
    ci = x
    zi = zr = 0.0
    i = 0
    while true
      i += 1
      temp = zr * zi
      zr2 = zr * zr
      zi2 = zi * zi
      zr = zr2 - zi2 + cr
      zi = temp + temp + ci
      return i if zi2 + zr2 > BAILOUT
      return 0 if i > MAX_ITERATIONS
    end
  end
end
 
time = Time.now
Bench1.new
puts "nRuby Elapsed %.3f" % (Time.now - time)

Perl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Ported from C to Perl by Anders Bergh <anders1@gmail.com>
 
$BAILOUT=16;
$MAX_ITERATIONS=1000;
 
$begin = time();
 
sub mandelbrot {
       local $x = $_[0];
       local $y = $_[1];
 
       local $cr = $y - 0.5;
       local $ci = $x;
       local $zi = 0.0;
       local $zr = 0.0;
       local $i = 0;
 
       while (1)
       {
               $i = $i + 1;
               local $temp = $zr * $zi;
               local $zr2 = $zr * $zr;
               local $zi2 = $zi * $zi;
               $zr = $zr2 - $zi2 + $cr;
               $zi = $temp + $temp + $ci;
               if ($zi2 + $zr2 > $BAILOUT)
               {
                       return $i;
               }
               if ($i > $MAX_ITERATIONS)
               {
                       return 0;
               }
       }
}
 
for ($y = -39; $y < 39; $y++)
{
       print("n");
       for ($x = -39; $x < 39; $x++)
       {
               $i = mandelbrot($x/40.0, $y/40.0);
               if ($i == 0)
               {
                       print("*");
               }
               else
               {
                       print(" ");
               }
       }
}
print("n");
 
$end = time() - $begin;
 
printf ("Perl Elapsed %.3fn",$end);

Comments:

The speed of Lua isn’t a news: only 5 times slower than compiled Java code, the best result. Its simplicity is its strength, maybe that’s what makes it so fast? It was adopted by Blizzard in the videogame “World of Warcraft” and if they did so, with no doubt, there is a reason. It is not object-oriented, does not natively support objects even if there is a project LOOP that extends this programming model.
PHP and Perl does not need comments.
Among the C version of Ruby and Python is clearly faster the second. The fair comparison would be the Rb1.8.6 with Py2.6.2 and Rb1.9.1 with Py3.1.1.
The challenge between the versions that uses the Java VM: Groovy, Jython and JRuby, sees the last as the winner. Groovy is far behind as performance in this test but the biggest question I have is: who is it for? As syntax is not bad but, imho, Ruby is even more fluent and then have that rake that is so convenient for many things.
The challenge between the .NET versions sees IronPython incredibly forward! But what they put in, the dynamite? It will be very interesting to examine the new ASP.NET MVC framework recently arrived at version 1 and that will be included in the framework. Net 4, there are projects for both IronRuby IronPython.
If this article was interesting, perhaps you can find something else through the advertisements of my sponsor, is in the right column, thanks!
Bye!

 

Five rubies in the multicore challenge

24 Aug

The system on which I performed the test is a laptop: Dell Inspiron 9400 with Centrino Duo, Intel T7200 4MB Cache 2GHz (166×12) 2GB Ram 667Mhz. This is a physical system Windows XP Pro SP3.

The purpose: to check the exploitation of more processor cores (two in my case) by comparing a single process with a double execution simultaneously.

To do this I used the string’s bench used in a previous article .
It must be remembered that the implementation of the dual process, everyone must share the CPU with the operating system (the load is always 100%) while in the single test that does not happen, a core is dedicated only to the test. For this reason, the operating system didn’t run antivirus or other heavy processes.

Let’s start with the oldest interpreter:

Ruby 1.8.6 patch 111

C:LavoroProgettiTestBench>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      6.641000   0.000000   6.641000 (  6.640000)
<<                     5.344000   0.000000   5.344000 (  5.375000)
#{}                    6.078000   0.000000   6.078000 (  6.078000)
Add 100.000:
+=                     3.547000   2.969000   6.516000 (  6.579000)
<<                     0.062000   0.000000   0.062000 (  0.062000)
a = a + '.'            3.000000   3.422000   6.422000 (  6.547000)
#{}                    4.531000   1.906000   6.437000 (  6.516000)
Other 100.000:
* 100:                 0.500000   0.063000   0.563000 (  0.562000)
capitalize:            1.719000   0.078000   1.797000 (  1.797000)
upcase:                4.906000   0.140000   5.046000 (  5.047000)
chomp:                 0.266000   0.063000   0.329000 (  0.328000)
include:               1.234000   0.000000   1.234000 (  1.234000)
index:                 1.235000   0.000000   1.235000 (  1.250000)
sub:                   0.875000   0.094000   0.969000 (  0.969000)
gsub:                 17.453000   0.562000  18.015000 ( 18.047000)
[x..y]:                0.547000   0.016000   0.563000 (  0.562000)
slice:                 0.562000   0.000000   0.562000 (  0.563000)
strip:                 0.156000   0.000000   0.156000 (  0.156000)
Each:                 12.282000   0.078000  12.360000 ( 12.390000)
Cast 1.000.000:
.to_i:                 0.437000   0.000000   0.437000 (  0.438000)
.to_sym:               0.531000   0.000000   0.531000 (  0.531000)
split:                 9.047000   0.140000   9.187000 (  9.235000)
--- Total:            80.953000   9.531000  90.484000 ( 90.906000)
Ruby 1.8.6 patch 111

Ruby 1.8.6 patch 111

Single thread:

--- Total:            80.953000   9.531000  90.484000 ( 90.906000)

Double simultaneous execution:

CORE1:
--- Total:            87.063000  19.531000 106.594000 (107.923000)

CORE2:
--- Total:            91.344000  18.375000 109.719000 (110.125000)

This is the decrease of the double simultaneous execution:
Decrease by 10%
Real decrease of 20%

Ruby 1.8.6 patch 368

C:LavoroProgettiTestBench>ruby -v
ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      4.828000   0.000000   4.828000 (  4.828125)
<<                     3.938000   0.000000   3.938000 (  3.953125)
#{}                    4.719000   0.016000   4.735000 (  4.750000)
Add 100.000:
+=                     3.687000   2.719000   6.406000 (  6.468750)
<<                     0.047000   0.000000   0.047000 (  0.046875)
a = a + '.'            3.422000   3.000000   6.422000 (  6.468750)
#{}                    4.797000   1.109000   5.906000 (  5.906250)
Other 100.000:
* 100:                 0.328000   0.094000   0.422000 (  0.421875)
capitalize:            0.890000   0.078000   0.968000 (  1.000000)
upcase:                3.469000   0.109000   3.578000 (  3.578125)
chomp:                 0.235000   0.078000   0.313000 (  0.312500)
include:               0.796000   0.000000   0.796000 (  0.796875)
index:                 0.797000   0.000000   0.797000 (  0.796875)
sub:                   0.750000   0.172000   0.922000 (  0.921875)
gsub:                 20.860000   0.531000  21.391000 ( 21.421875)
[x..y]:                0.437000   0.016000   0.453000 (  0.453125)
slice:                 0.438000   0.000000   0.438000 (  0.437500)
strip:                 0.109000   0.000000   0.109000 (  0.109375)
Each:                  9.078000   0.172000   9.250000 (  9.281250)
Cast 1.000.000:
.to_i:                 0.297000   0.000000   0.297000 (  0.296875)
.to_sym:               0.344000   0.000000   0.344000 (  0.343750)
split:                 7.375000   0.109000   7.484000 (  7.500000)
--- Total:            71.641000   8.203000  79.844000 ( 80.093750)
Ruby 1.8.6 patch 368

Ruby 1.8.6 patch 368

Single thread:

--- Total:            71.641000   8.203000  79.844000 ( 80.093750)

Double simultaneous execution:

CORE1:
--- Total:            80.797000  17.750000  98.547000 ( 99.093750)
CORE2:
--- Total:            76.500000  19.375000  95.875000 ( 97.187500)

Decrease of 9,8%
Real decrease of 22,5%

Ruby 1.9.1

C:LavoroProgettiTestBench>ruby -v
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]

C:LavoroProgettiTestBench>ruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      2.610000   0.000000   2.610000 (  2.609375)
<<                     2.125000   0.000000   2.125000 (  2.125000)
#{}                    2.796000   0.000000   2.796000 (  2.796875)
Add 100.000:
+=                     3.688000   2.875000   6.563000 (  6.609375)
<<                     0.031000   0.000000   0.031000 (  0.031250)
a = a + '.'            3.235000   3.265000   6.500000 (  6.578125)
#{}                    5.187000   2.266000   7.453000 (  7.546875)
Other 100.000:
* 100:                 0.203000   0.125000   0.328000 (  0.328125)
capitalize:            5.563000   0.047000   5.610000 (  5.656250)
upcase:                0.703000   0.031000   0.734000 (  0.734375)
chomp:                 0.219000   0.063000   0.282000 (  0.296875)
include:               0.156000   0.000000   0.156000 (  0.156250)
index:                 0.156000   0.000000   0.156000 (  0.156250)
sub:                   0.500000   0.093000   0.593000 (  0.593750)
gsub:                  8.453000   0.266000   8.719000 (  8.718750)
[x..y]:                0.219000   0.000000   0.219000 (  0.218750)
slice:                 0.219000   0.000000   0.219000 (  0.218750)
strip:                 0.109000   0.000000   0.109000 (  0.109375)
Each:                  7.188000   0.141000   7.329000 (  7.328125)
Cast 1.000.000:
.to_i:                 0.234000   0.000000   0.234000 (  0.234375)
.to_sym:               0.391000   0.000000   0.391000 (  0.390625)
split:                 4.609000   0.000000   4.609000 (  4.609375)
--- Total:            48.594000   9.172000  57.766000 ( 58.046875)
Ruby 1.9.1 patch 129

Ruby 1.9.1 patch 129

Single thread:

--- Total:            48.594000   9.172000  57.766000 ( 58.046875)

Double simultaneous execution:

CORE1:
--- Total:            56.345000  19.219000  75.564000 ( 76.312500)
CORE2:
--- Total:            52.109000  20.703000  72.812000 ( 74.156250)

Decrease of 11,5%
Real decrease of 29,6%

jRuby 1.3.1

C:LavoroProgettiTestBench>jruby -v
jruby 1.3.1 (ruby 1.8.6p287) (2009-07-24 6586) (Java HotSpot(TM) Client VM 1.6.0
_15) [x86-java]

C:LavoroProgettiTestBench>jruby bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      1.703000   0.000000   1.703000 (  1.672000)
<<                     1.375000   0.000000   1.375000 (  1.375000)
#{}                    0.625000   0.000000   0.625000 (  0.625000)
Add 100.000:
+=                     8.343000   0.000000   8.343000 (  8.343000)
<<                     0.031000   0.000000   0.031000 (  0.031000)
a = a + '.'            8.406000   0.000000   8.406000 (  8.406000)
#{}                   18.984000   0.000000  18.984000 ( 18.984000)
Other 100.000:
* 100:                 0.157000   0.000000   0.157000 (  0.157000)
capitalize:            0.937000   0.000000   0.937000 (  0.937000)
upcase:                1.703000   0.000000   1.703000 (  1.703000)
chomp:                 0.063000   0.000000   0.063000 (  0.063000)
include:               0.281000   0.000000   0.281000 (  0.281000)
index:                 0.313000   0.000000   0.313000 (  0.313000)
sub:                   0.437000   0.000000   0.437000 (  0.437000)
gsub:                  4.813000   0.000000   4.813000 (  4.813000)
[x..y]:                0.156000   0.000000   0.156000 (  0.156000)
slice:                 0.140000   0.000000   0.140000 (  0.140000)
strip:                 0.047000   0.000000   0.047000 (  0.047000)
Each:                  3.094000   0.000000   3.094000 (  3.094000)
Cast 1.000.000:
.to_i:                 0.609000   0.000000   0.609000 (  0.609000)
.to_sym:               0.266000   0.000000   0.266000 (  0.266000)
split:                 2.203000   0.000000   2.203000 (  2.203000)
--- Total:            54.686000   0.000000  54.686000 ( 54.655000)
jRuby 1.3.1 => 1.8.6 p287

jRuby 1.3.1 => 1.8.6 p287

Single thread:

--- Total:            54.686000   0.000000  54.686000 ( 54.655000)

Double simultaneous execution:

CORE1:
--- Total:            65.170000   0.000000  65.170000 ( 64.920000)
CORE2:
--- Total:            64.234000   0.000000  64.234000 ( 64.000000)

Decrease of 17,2%
Real decrease of 17,9%

IronRuby 0.9.0

C:LavoroProgettiTestBench>ir -v
IronRuby 0.9.0.0 on .NET 2.0.0.0

C:LavoroProgettiTestBench>ir bench_str.rb
                           user     system      total        real
Concat 1.000.000:
+                      3.562500   0.000000   3.562500 (  3.234375)
<<                     1.531250   0.000000   1.531250 (  1.531250)
#{}                    1.453125   0.015625   1.468750 (  1.453125)
Add 100.000:
+=                    54.656250  11.296875  65.953125 ( 66.171875)
<<                     0.031250   0.000000   0.031250 (  0.031250)
a = a + '.'           54.734375  11.718750  66.453125 ( 66.671875)
#{}                   55.609375  10.843750  66.453125 ( 66.609375)
Other 100.000:
* 100:                 0.671875   0.000000   0.671875 (  0.671875)
capitalize:            2.593750   0.000000   2.593750 (  2.609375)
upcase:                7.484375   0.000000   7.484375 (  7.500000)
chomp:                 0.250000   0.000000   0.250000 (  0.250000)
include:               5.968750   0.015625   5.984375 (  6.031250)
index:                 5.968750   0.031250   6.000000 (  6.031250)
sub:                   1.781250   0.015625   1.796875 (  1.781250)
gsub:                 12.203125   0.031250  12.234375 ( 12.281250)
[x..y]:                0.406250   0.000000   0.406250 (  0.390625)
slice:                 0.343750   0.000000   0.343750 (  0.343750)
strip:                 0.078125   0.000000   0.078125 (  0.078125)
Each:                 31.546875   0.312500  31.859375 ( 32.203125)
Cast 1.000.000:
.to_i:                 0.265625   0.000000   0.265625 (  0.250000)
.to_sym:               0.406250   0.015625   0.421875 (  0.421875)
split:                 5.515625   0.031250   5.546875 (  5.531250)
--- Total:           247.062500  34.328125 281.390625 (282.078125)

C:LavoroProgettiTestBench>
IronRuby 0.9.0

IronRuby 0.9.0

Single thread:

--- Total:           247.062500  34.328125 281.390625 (282.078125)

Double simultaneous execution:

CORE1:
--- Total:           315.531250  40.578125 356.109375 (359.500000)
CORE2:
--- Total:           300.484375  46.156250 346.640625 (350.265625)

Decrease of 24,5%
Real decrease of 25,8%

Summary

Ruby 1.8.6 patch 111
Decrease of 10%
Real decrease of 20%

Ruby 1.8.6 patch 368
Decrease of 9,8%
Real decrease of 22,5%

Ruby 1.9.1
Decrease of 11,5%
Real decrease of 29,6%

jRuby 1.3.1
Decrease of 17,2%
Real decrease of 17,9%

IronRuby 0.9.0
Decrease of 24,5%
Real decrease of 25,8%

Conclusions

In this test, JRuby is the only one that has a uniform load on both cores and it does almost constantly for all the time. Only at the end moves towards a core but with more discretion. Also good performance with the two processes simultaneously.

Versions 1.8.6 and 1.9.1 are quite aligned and able to share the load solely on test "Add". The performance of the dual process would not be bad but creates problems for the system (at least mine) which prolongs the time.

IronRuby has some trouble with the strings already highlighted in a previous article. Ignoring this issue, the second core was left unloaded for all the duration. With the second core free, I would have expected a less pronounced decrease in the test process twice but it did not happen, this is strange.

In a future article, I'd like to repeat the test on a server quad core.

 
Comments Off

Posted in IronRuby .NET, JRuby, Ruby

 

FreeTTS, a java library used from jRuby on Rails

23 Aug

In this article we will see how to use the Java classes contained in a JAR file.
For this purpose we use a nice open source library developed by Carnegie Mellon University: FreeTTS.
The acronym TTS means Text To speech, makes it possible to transform a text into audio format. We’ll use it in a jRuby on Rails’s project to make us read the text we enter in the db.

In a previous article, we have seen how to configure the environment, so let’s start creating the new application:


C:>rails ProvaFreeTTS

In the lib folder from the root, create a sub folder freetts. Download the file freetts-1.2.2freetts-1.2.2-bin.zip, unpack the contents to a temporary folder, copy only the contents of the folder lib (jar files and JSAPI) into the dir just created: your_applibfreetts.

Now we create the jar’s interface to the jar:

#libfreetts.rb
 
require 'freetts/freetts.jar'
 
import com.sun.speech.freetts.Voice
import com.sun.speech.freetts.VoiceManager
import com.sun.speech.freetts.util.Utilities
 
class FreeTTS
  def initialize
    @voice = VoiceManager.getInstance.getVoice(Utilities.getProperty("voice16kName","kevin16"))
    @voice.allocate
  end
 
  def speak(txt=nil)
    return nil unless txt
    @voice.speak txt
  end
end

Let’s create a simple resource “sentence” with a single field “body“.


C:ProvaFreeTTS>jruby script/generate scaffold sentence body:text

Now we create two new operations, as I explained in detail in a previous article.

We start from the controller by adding at the bottom :

  #appcontrollerssentences_controller.rb
 
  def speak
    @sentence = Sentence.new(params[:sentence])
    require 'freetts'
    tts = FreeTTS.new
    tts.speak @sentence.body
    render (@sentence.new_record? ? :new : :edit)
  end
 
  def read
    @sentence = Sentence.find(params[:id])
    require 'freetts'
    tts = FreeTTS.new
    tts.speak @sentence.body
    redirect_to :back
  end

Now it’s the views turn.

We create a new partial where we’ll move into the resource’s data form, so to have only one form for every operation:

#appviewssentence_sentence.html.erb
 
<% form_for(@sentence) do |f| %>
  <%= f.error_messages %>
 
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>
 
<h2>Preview</h2>
<% form_for @sentence, :url => speak_sentences_path, :method => :put do |f| %>
  <%= f.error_messages %>
 
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit 'Speak' %>
  </p>
<% end %>

Add a clone of the form created by the scaffold but with a different action, to call the transaction speak.

Now modify the views created automatically:

#appviewssentencenew.html.erb
 
<h1>New sentence</h1>
 
<%= render @sentence %>
 
<%= link_to 'Back', sentences_path %>
 
#appviewssentenceedit.html.erb
 
<h1>Editing sentence</h1>
 
<%= render @sentence %>
 
<%= link_to 'Show', @sentence %> |
<%= link_to 'Back', sentences_path %>

Now, we modify the index in order to retrieve the second operation: the “read”. Basically, clicking on the corresponding line, will read the text previously stored in the db.

We only need to add a line to obtain this view:

#appviewssentenceindex.html.erb
<h1>Listing sentences</h1>
 
<table>
  <tr>
    <th>Body</th>
  </tr>
 
<% @sentences.each do |sentence| %>
  <tr>
    <td><%=h sentence.body %></td>
    <td><%= link_to 'Read', read_sentence_path(sentence) %></td>
    <td><%= link_to 'Show', sentence %></td>
    <td><%= link_to 'Edit', edit_sentence_path(sentence) %></td>
    <td><%= link_to 'Destroy', sentence, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>
 
<br />
 
<%= link_to 'New sentence', new_sentence_path %>

We modify the routing:

#configroutes.rb
 
#change map.resources :sentences with
map.resources :sentences, :member => { :read => :get }, :collection => { :speak => :put }
 
#add
map.root :controller => "sentences"

Configure the database.yml file, we create the db and tables with rake and start up Glassfish as explained in this previous article (find database.yml).

As we have seen, using java libraries with jruby on rails is very simple, we are ready to make our server to say the most dirty words!

The project can be downloaded from here.

Enjoy!

 
Comments Off

Posted in JRuby, Ruby on Rails

 

Setting up an environment for jRuby on Rails

20 Aug

My apologize in advance for having used a robot translator, a small help to save a little time:

Over the next few articles i will use jruby 1.3.1 with Rails 2.3.3 and this is an opportunity to write two lines about how to configure the system.
By the way, the system used is a virtualized windows xp, java sdk 6, Glassfish as application server and javadb.

Download and install the java sdk, at the moment we are at version 6 update 14.

There is also the sdk with NetBeans, an excellent IDE to manage projects ruby on rails. It could be an opportunity to try it. The package also includes the application server Glassfish V3.

Now we download and install jRuby, atm we are at version 1.3.1.

Installation is simple: unpack the zip in a path like C:rubyjruby-131

Let’s set the environment variables, right click on “My Computer” icon on the desktop -> Properties -> select the tab “Advanced” -> click on “environment variables”.

  • Add to the PATH variable the path to the bin folder where we installed jruby, for example C:rubyjruby-131bin.
  • Set the environment variable JAVA_HOME. In the “system variables” -> “New” -> as the name “JAVA_HOME” value as the installation path dell’sdk for example C:ProgrammiJavajdk1.6.0_14

It is not necessary to restart the operating system, we test the result:

C:>jruby -v
jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java]

Eureka! Now we’re ready to use jruby, jirb and gem. If in your system there is also classic ruby you may specify jruby-S to recall gem otherwise you can also put jruby path in the last position inside environment variable. Ok, now we can install rails 2.3.3:

C:>jruby -S gem install rails
Successfully installed activesupport-2.3.3
Successfully installed activerecord-2.3.3
Successfully installed actionpack-2.3.3
Successfully installed actionmailer-2.3.3
Successfully installed activeresource-2.3.3
Successfully installed rails-2.3.3
6 gems installed
Installing ri documentation for activesupport-2.3.3...
Installing ri documentation for activerecord-2.3.3...
Installing ri documentation for actionpack-2.3.3...
Installing ri documentation for actionmailer-2.3.3...
Installing ri documentation for activeresource-2.3.3...
Installing ri documentation for rails-2.3.3...
Installing RDoc documentation for activesupport-2.3.3...
Installing RDoc documentation for activerecord-2.3.3...
Installing RDoc documentation for actionpack-2.3.3...
Installing RDoc documentation for actionmailer-2.3.3...
Installing RDoc documentation for activeresource-2.3.3...
Installing RDoc documentation for rails-2.3.3...

We install the jdbc adapter to connect to Java DB (i don’t use jruby-S anymore, in my system is not necessary):

C:>gem install activerecord-jdbcderby-adapter
Successfully installed activerecord-jdbc-adapter-0.9.1
Successfully installed jdbc-derby-10.4.2.0
Successfully installed activerecord-jdbcderby-adapter-0.9.1
3 gems installed
Installing ri documentation for activerecord-jdbc-adapter-0.9.1...
Installing ri documentation for jdbc-derby-10.4.2.0...
Installing ri documentation for activerecord-jdbcderby-adapter-0.9.1...
Installing RDoc documentation for activerecord-jdbc-adapter-0.9.1...
Installing RDoc documentation for jdbc-derby-10.4.2.0...
Installing RDoc documentation for activerecord-jdbcderby-adapter-0.9.1...

Then install the gem for Glassfish:

C:>gem install glassfish
Successfully installed rack-1.0.0
Successfully installed glassfish-0.9.5-universal-java
2 gems installed
Installing ri documentation for rack-1.0.0...
Installing ri documentation for glassfish-0.9.5-universal-java...
Installing RDoc documentation for rack-1.0.0...
Installing RDoc documentation for glassfish-0.9.5-universal-java...

We are ready, we create a new project:

C:>rails ProvaArticolo

… in the database’s configuration file we use:

# config/database.yml
 
# JavaDB Setup
#
# You may need to copy derby.jar into
#  TODO: location C:rubyjruby-131lib
# With Java SE 6 and later this is not necessary.
development:
  adapter: derby
  database: db/development.db
 
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: derby
  database: db/test.db
 
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
production:
  adapter: derby
  database: db/production.db
 
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.

Let us start the server, from the prompt inside our application’s directory:

C:ProvaArticolo>glassfish
Starting GlassFish server at: 127.0.0.1:3000 in development environment...
Writing log messages to: C:/ProvaArticolo/log/development.log.
Press Ctrl+C to stop.

At http://localhost:3000/ should see the welcome page.
Now stop the server with Ctrl + C db and create the environment in development:

C:ProvaArticolo>rake db:create
(in C:/ProvaArticolo)
db/development.db already exists

We create something within the database, we start with the source of rails:

C:ProvaArticolo>jruby script/generate scaffold article name:string body:text
create app/models/
exists app/controllers/
exists app/helpers/
exists app/views/articles
create app/views/layouts/
create test/functional/
create test/unit/
create test/unit/helpers/
create public/stylesheets/
create app/views/articles/index.html.erb
create app/views/articles/show.html.erb
create app/views/articles/new.html.erb
create app/views/articles/edit.html.erb
create app/views/layouts/articles.html.erb
create public/stylesheets/scaffold.css
create app/controllers/articles_controller.rb
create test/functional/articles_controller_test.rb
create app/helpers/articles_helper.rb
create test/unit/helpers/articles_helper_test.rb
route map.resources :articles
dependency model
exists app/models/
exists test/unit/
create test/fixtures/
create app/models/article.rb
create test/unit/article_test.rb
create test/fixtures/articles.yml
exists db/migrate
create db/migrate/20090729171105_create_articles.rb

and create the table:

C:ProvaArticolo>rake db:migrate
(in C:/ProvaArticolo)
== CreateArticles: migrating =================================================
-- create_table(:articles)
-> 0.0700s
-> 0 rows
== CreateArticles: migrated (0.0700s) ========================================

Finally remove the index.html file from inside the public folder and create the initial route:

#configroutes.rb
map.root :controller => "articles"

Reboot the server and at http://localhost:3000/ this time we should see a list of articles.

If I don’t have forgotten something, jruby on rails is ready, enjoy!

 
1 Comment

Posted in Ruby

 

Add an operation to a resource in a RESTful system

20 Aug

My apologize in advance for having used a robot translator, a small help to save a little time:

To create a RESTful application with rails is very simple:

C:>rails MyApp

In a REST system, the application consists of Web resources with a set of linked transactions. With the scaffold generate a resource and the four basic functions to manage it, called CRUD (Create, Read, Update, Delete)

C:>script/generate scaffold assets name:string

We created the base from which to test this short article.

Suppose you want to add a new operation to our resource Asset, for example we want to handle the copy. We start by creating a new address by changing the scaffold created by:

#config/routes.rb
map.resources :assets, :member => { :copy => :get }

Let’s check the results:
C:>rake routes

…ensuring that there is the new path:

copy_asset GET /assets/:id/copy(.:format) {:controller=>"assets", :action=>"copy"}

The copy is useful in many contexts because it allows for the inclusion of a new resource from an existing.
Translate into code, and let’s create the new controller:

#app/controllers/assets_controller.rb
 
# GET /assets/copy
# GET /assets/copy.xml
def copy
  @asset = asset.new
  @asset.attributes = Asset.find(params[:id]).attributes
 
  respond_to do |format|
    format.html { render :new }
    format.xml  { render :xml => @asset }
  end
end

You can also create the view, depends on the transaction that has to be managed. This example uses the view of the new as they are very similar and both the recall: create.

Create or not create a new action on the resource is pure philosophy, leaving aside the question how we create a new target, we refer to a collection of resources:

#config/routes.rb
map.resources :assets, :member => { :copy => :get }, :collection => { :createcp => :put }

I used a collection because in this case does not need a reference to an existing resource, if I had used: the member for the path generation dell’uri would have required.

if we test again the routes should add this:

createcp_asset PUT /assets/createcp(.:format) {:controller=>"assets", :action=>"createcp"}

then we create the new view and form with the uri of the new action and specify the method (for detail put but I indicated to use an equivalent post):

#app/views/assets/copy.html.erb
 
<% form_for(@asset, :url => createcp_assets_path, :method=>:put do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label t(:name) %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit t(:create) %>
  </p>
<% end %>

and the new action in the controller:

#app/controllers/assets_controller.rb
 
# POST /assets/createcp
# POST /assets/createcp.xml
def createcp
  @asset = Asset.new(params[:asset])
 
  #do something
 
  respond_to do |format|
    if @asset.save
      flash[:notice] = I18n.t(:created, :model => I18n.t('model.asset'))
      format.html { redirect_to assets_path }
      format.xml  { render :xml => @asset, :status => :created, :location => @asset }
    else
      format.html { render :action => "copy" }
      format.xml  { render :xml => @asset.errors, :status => :unprocessable_entity }
    end
  end
end

To invoke the new operation by the index:

#app/views/assets/index.html.erb
<%= link_to t(:copy), copy_asset_path(asset) %>

Finally, these are the words used in the example for internationalization (if you need it):

create: "Crea"
copy: "Copia"
created: "The item {{model}} was created successfully."
name: "Name"
model:
  asset: "Asset"
 
Comments Off

Posted in Ruby on Rails