<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>## Rake Task [ruby_on_rails]

def render_page_sitemap(scope, sitemapname,statics, machines, lang)
    file = RAILS_ROOT + "/public/"+ sitemapname+"_"+lang+".xml.gz"
    Zlib::GzipWriter.open(file) do |gzip|
        xml = Builder::XmlMarkup.new(:target =&gt; gzip)
        xml.instruct!
        xml.urlset("xmlns:xsi" =&gt; 'http://www.w3.org/2001/XMLSchema-instance', "xsi:schemaLocation" =&gt;"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd", "xmlns" =&gt; "http://www.sitemaps.org/schemas/sitemap/0.9") do
            statics.each do |page|
                xml.url do
                    xml.loc(url_for(:only_path =&gt; false, :controller =&gt; "homepages", :action =&gt; page, :lang =&gt; lang, :scope =&gt; scope))
                    xml.lastmod(Time.now.strftime('%Y-%m-%d'))
                    xml.changefreq("daily")
                end
            end

            machines.each do |mach|
                xml.url do
                    xml.loc(url_for(:only_path =&gt; false, :controller =&gt; "homepages", :action =&gt; 'showmach', :id =&gt; mach, :lang =&gt; lang, :scope =&gt; scope))
                    xml.lastmod(DateTime.strptime(mach.created, '%Y-%m-%d %H:%M:%S').strftime("%Y-%m-%dT%H:%M:%S+00:00"))
                    xml.changefreq("monthly")
                end
            end
        end #XML
        gzip.close
    end #GZIP
end

def render_overall_sitemap(sitemapname, baseurl)
    file = RAILS_ROOT + "/public/overall_"+sitemapname+".xml.gz"
    Zlib::GzipWriter.open(file) do |gzip|
        xml = Builder::XmlMarkup.new(:target =&gt; gzip)
        xml.instruct!
        xml.sitemapindex("xmlns:xsi" =&gt; 'http://www.w3.org/2001/XMLSchema-instance', "xsi:schemaLocation" =&gt;"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd", "xmlns" =&gt; "http://www.sitemaps.org/schemas/sitemap/0.9") do
            xml.sitemap do
                xml.loc("http://#{baseurl}/#{sitemapname}_de.xml.gz")
                xml.lastmod(Time.now.strftime('%Y-%m-%d'))
            end
            xml.sitemap do
                xml.loc("http://#{baseurl}/#{sitemapname}_en.xml.gz")
                xml.lastmod(Time.now.strftime('%Y-%m-%d'))
            end
            xml.sitemap do
                xml.loc("http://#{baseurl}/#{sitemapname}_zh.xml.gz")
                xml.lastmod(Time.now.strftime('%Y-%m-%d'))
            end
            xml.sitemap do
                xml.loc("http://#{baseurl}/#{sitemapname}_ru.xml.gz")
                xml.lastmod(Time.now.strftime('%Y-%m-%d'))
            end
        end #XML
        gzip.close
    end #GZIP
end




task :generate_sitemap =&gt; :environment do
    include ActionController::UrlWriter  # for url_for

    # IMTM Sitemaps
    default_url_options[:host] = 'www.imtm.com'
    staticpages = Array.new
    staticpages &lt;&lt; "index"
    staticpages &lt;&lt; "impressum"
    staticpages &lt;&lt; "kontakt"
    staticpages &lt;&lt; "wir"

    machines = Machine.find(:all, :select =&gt; "machines.id, machines.created, model, manufacturer_id", :include =&gt; [:manufacturer])
    render_page_sitemap("imtm", "imtm_sitemap",staticpages, machines, "de")
    render_page_sitemap("imtm", "imtm_sitemap",staticpages, machines, "en")
    render_page_sitemap("imtm", "imtm_sitemap",staticpages, machines, "zh")
    render_page_sitemap("imtm", "imtm_sitemap",staticpages, machines, "ru")
    
    render_overall_sitemap("imtm_sitemap", "www.imtm.com")

    # globus Sitemaps
    default_url_options[:host] = 'www.globus-trading.com'
    staticpages = Array.new
    staticpages &lt;&lt; "index"
    staticpages &lt;&lt; "anfahrt"
    staticpages &lt;&lt; "anfrage"
    staticpages &lt;&lt; "downloads"
    staticpages &lt;&lt; "impressum"
    staticpages &lt;&lt; "kontakt"
    staticpages &lt;&lt; "produktionslinien"
    staticpages &lt;&lt; "wir"

    machines = Machine.find(:all, :select =&gt; "machines.id, machines.created, model, manufacturer_id", :conditions =&gt; ["globushp = 1 OR globusspecial = 1 OR globusstart = 1 OR globusstartspecial = 1"], :include =&gt; [:manufacturer])
    render_page_sitemap("globus", "globus_sitemap",staticpages, machines, "de")
    render_page_sitemap("globus", "globus_sitemap",staticpages, machines, "en")
    render_page_sitemap("globus", "globus_sitemap",staticpages, machines, "zh")
    render_page_sitemap("globus", "globus_sitemap",staticpages, machines, "ru")
    
    render_overall_sitemap("globus_sitemap", "www.globus-trading.com")
 
end</code>
    <comment>Hi,

I'm looking for a way to speed up mail RoR Rake Task below :
(start reading the code at "task :generate_sitemap =&gt; :environment")

### The Problem is: ###
the Sitemaps I'm generating are very large (&gt; 35000 URLs) and each of the Sitemaps comes in 4 languages
--&gt; Sitemap generation takes several minutes

### Optimizations I've already done ###
- installed the "fast_xs" gem to speed up the whole rails XML Builder
- "pipeing" the generated xml directly into the gzip files to (hopefully) save some memory
  (see "xml = Builder::XmlMarkup.new(:target =&gt; gzip)")

Does any of you have any other clever idea how to speed up the code? I'd really appreciate any sort of help :-)</comment>
    <created-at type="datetime">2010-02-14T16:35:06+00:00</created-at>
    <id type="integer">1178</id>
    <language>Ruby</language>
    <permalink>speeding-up-sitemap-generation</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Speeding up Sitemap generation</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2010-02-23T23:55:56+00:00</updated-at>
    <user-id type="integer">1933</user-id>
    <user>
      <id type="integer">1933</id>
      <identity-url>http://tiekuhn.myopenid.com</identity-url>
      <name>tiekuhn</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>## Javascript [jquery_javascript]
;(function($) {
        $.fn.extend(
            {
                multiselect: function(options) {
                    var options = $.extend({
                            changeCallback: null,
                            single: false,
                        },
                        options || {}
                    );

                    return this.each(function(){
                            $(this).multiselect_render(options);
                        }
                    )
                },
                multiselect_render: function(options) {
                    var $selected_elements = $('&lt;span&gt;&lt;/span&gt;');

                    var $options = $('&lt;div&gt;&lt;/div&gt;');

                    var $search = $('&lt;input type="text"/&gt;')
                    .addClass('multiselectSearch')
                    .keyup(function(e) {
                            var _val = $(this).val();

                            if(!_val) {
                                $options
                                .children("div")
                                .show();
                            } else {
                                $options
                                .children("div")
                                .hide()
                                .filter(":icontains("+_val+")")
                                .show();
                            }
                        }
                    );

                    var $widget = $("&lt;div/&gt;") ;

                    var $choose;
                    if(options.single) {
                        $choose = $search
                    } else {
                        $choose = $("&lt;a/&gt;")
                        .addClass('multiselectChoose')
                        .text("[Choose...]");
                    }
                    $choose.click(function(e) {
                            e.preventDefault();
                            $widget.toggle(
                                0,
                                function() {
                                    $(document).click(body_click_hide_widget);
                                }
                            );
                            $search.select();
                        }
                    );

                    var body_click_hide_widget = function(e) {
                        var $$this = $(e.target);

                        if($$this[0] == $choose[0])
                            return;

                        var $$parents = $$this.parents().andSelf();

                        hide = true;
                        for (var i=0; i&lt;$$parents.length; i++) {
                            if($$parents[i] == $widget[0]) {
                                hide=false;
                                break;
                            }
                        }
                        if(hide) {
                            $widget.hide();
                            $(document).unbind("click", body_click_hide_widget);
                        }
                    }

                    var update_selected_elements = function() {
                        var arr = new Array();
                        $options.find(":checked").next('label').each(function() {
                                arr.push($(this).text());
                            }
                        );
                        $selected_elements.text(arr.join(', '));
                    };

                    $widget.append($search);

                    // button close
                    if(!options.single) {
                        $widget
                        .append(
                            $("&lt;a/&gt;")
                            .text("close")
                            .addClass('multiselectClose')
                            .click(function(e) {
                                    e.preventDefault();
                                    $widget.hide();
                                    $(document).unbind("click", body_click_hide_widget);
                                }
                            )
                        )
                    }

                    var $this = $(this).hide();
                    var $opts = $this.children("option");
                    var $parent_id = $this.attr('id');
                    var $parent_name = $this.attr('name');
                    var nb_opts = $opts.length;
                    var type = options.single ? 'radio' : 'checkbox';
                    var opts_arr = new Array();
                    $opts.each(
                        function(idx) {
                            var $$this = $(this);
                            var $$this_value = $$this.val()

                            var input = '&lt;input type="' + type + '" id="' + $parent_id + '-' + $$this_value + '" name="' + $parent_name + '" value="' + $$this_value + '"' + ($$this.is(':selected') ? ' checked=checked' : '') + ' /&gt;';

                            var label = '&lt;label for="' + $parent_id + '-' + $$this_value + '"&gt;' + $$this.text() + '&lt;/label&gt;';

                            var option = '&lt;div&gt;'+input+label+'&lt;/div&gt;';

                            opts_arr.push(option);
                        }
                    );

                    $options.append(opts_arr.join(''));

                    $options
                    .children('div').addClass('multiselectOption')
                    .change(
                        function(e) {
                            if(options.changeCallback) {
                                options.changeCallback.call(this);
                            }
                            update_selected_elements();
                            if(options.single) {
                                $search.val($selected_elements.text());
                                $widget.hide();
                                $(document).unbind("click", body_click_hide_widget);
                            }
                        }
                    );

                    update_selected_elements();

                    $widget.append($options);

                    if(options.single) {
                        $choose.val($selected_elements.text())
                        $this.before($choose);
                    } else {
                        $this.before($selected_elements).before($choose);
                    }

                    $this.replaceWith($widget.hide().addClass('multiselectWidget'));
                    return $widget
                }
            }
        );
    }
)(jQuery);</code>
    <comment>This is a little jQuery script I wrote in order to add a filter field to a selectbox (or multiselectbox). It works fine with few options but is painfully slow with 3000+ options. Any idea to refactor it ?

usage : $("select").multiselect({single: true});</comment>
    <created-at type="datetime">2010-02-03T23:05:22+00:00</created-at>
    <id type="integer">1168</id>
    <language>JavaScript</language>
    <permalink>filter-selectbox-with-3000-options</permalink>
    <refactors-count type="integer">2</refactors-count>
    <title>filter selectbox with 3000+ options </title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2010-02-26T07:54:11+00:00</updated-at>
    <user-id type="integer">1917</user-id>
    <user>
      <id type="integer">1917</id>
      <identity-url>http://akarzim.myopenid.com</identity-url>
      <name>akarzim</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website>http://akarz.im</website>
    </user>
  </code>
  <code>
    <code>&lt;?php
//class
	class Collection{
		private $data = array();
		public function data($key,$value=null){
			$keys = explode('.',$key);
			$ptr = &amp;$this-&gt;data;
			if(!is_null($value)){
				while(($key=array_shift($keys))!==null){
					if (!isset($ptr[$key])) 
						$ptr[$key] = array();
					$ptr = &amp;$ptr[$key];
				}
	
				$ptr[]=$value;
				return true;
			}
			else{
				while(($key=array_shift($keys))!==null){
					$ptr = &amp;$ptr[$key];
				}
				return array_shift($ptr);
			} 
		}		
	}
?&gt;
&lt;?php
//bench
	error_reporting(E_ALL ^ E_STRICT);
	$set = new Collection();
	$tests=500;
	$counter = 50;	
	$i=$deltas=0;
	$nums=array();
	$random_crap = array();
	
	for($i&lt;0;$i&lt;$tests;$i++){
		$iterator = 0;
		$begin = microtime(true);
		while($iterator &lt; $counter){
			 $set-&gt;data('random.numbers.put.in.a.really.deeply.nested.array',(int)mt_rand(0,$counter));
			 $iterator++;
		}
		while($iterator &gt; 0){
			 $nums[]=$set-&gt;data('random.numbers.put.in.a.really.deeply.nested.array');
			 $iterator--;
		}	
		$deltas+=(microtime(true)-$begin);		
	}
echo "On average it took $deltas seconds to do $tests tests. That's ".
	($tests/$deltas).
	" tests per second, or a total of ".
	(2*$tests*$counter).
	" writings and writings (50%/50%) at a rate of ".(($tests*$counter)/$deltas).
	" per second";	
?&gt;

&lt;pre&gt;&lt;?php var_dump($nums)?&gt;&lt;/pre&gt;</code>
    <comment>Hi everybody!
I'm kinda new here (been a lurker for long, but didn't feel like asking till right now), and I have a problem.

In my quest to stop @|#|\ repeating myself at work I decided to build a simple toolkit to pack all the things I did before and reuse them whenever needed. Thing is, most of my tools are based around this little booger here, which is an extremely barebones version of CakePHP's Set. What it does is essentially simple: 
Collection translates a dot.separated.key to a multidimensional array and puts or reads a given value from such array. 
What it also does is suck at speed: 
In the given bench on my development machine it does some 570-590 operations per second overall. Since it's the basement of everything else I built, it sounded like a good idea to make it generally faster (as much as possible) but my knowledge on this is really limited... Could you give me a hand here?</comment>
    <created-at type="datetime">2009-12-07T23:13:01+00:00</created-at>
    <id type="integer">1123</id>
    <language>PHP</language>
    <permalink>how-should-i-optimize-this-for-speed</permalink>
    <refactors-count type="integer">2</refactors-count>
    <title>How should I optimize this for speed?</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-12-08T15:39:51+00:00</updated-at>
    <user-id type="integer">1838</user-id>
    <user>
      <id type="integer">1838</id>
      <identity-url>http://claimid.com/cfvergara</identity-url>
      <name>blackBear</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>private static void Log(object message, MessageType type)
		{
			string str = message.ToString();

			//Handle newlines
			if (str.IndexOf('\n') &gt;= 0)
			{
				foreach (string s in str.Split('\n'))
					Log(s, type);
				return;
			}

			
			//Split the string recursively in smaller strings to fit the console window
			for (int i = str.Length; i &gt; 0; i--) //TODO: Optimize, adding a message of 1000 characters takes too long
			{
				if (MessageFits(str.Substring(0, i)))
				{
					messages.Add(new Message(str.Substring(0, i), type));

					//If user was already all scrolled down, keep him scrolled down
					if (shownMessage == messages.Count - 2)
						shownMessage = messages.Count - 1;

					Log(str.Substring(i), type); //Recursive call to handle the rest

					break;
				}
			}

		}

		private static bool MessageFits(string message)
		{
			return font.MeasureString(message).X &lt;= window.ClientBounds.Width - (padding * 2) - scrollBarWidth;
		}</code>
    <comment>This is a console logging function. I'm trying to get the lines to split if they don't fit the display (checked with MessageFits, included). However, the code takes a good second to execute with a message of 1000 characters or more. Is there any way this could be faster?</comment>
    <created-at type="datetime">2009-12-07T20:59:43+00:00</created-at>
    <id type="integer">1122</id>
    <language>C#</language>
    <permalink>line-splitting-optimization</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Line splitting optimization</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2010-01-19T18:40:42+00:00</updated-at>
    <user-id type="integer">1837</user-id>
    <user>
      <id type="integer">1837</id>
      <identity-url>http://xeon06.myopenid.com</identity-url>
      <name>xeon06.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>require 'net/sftp'

class Net::SFTP::Session
  def rm_r!(path)
    self.dir.entries(path).each do |entry|
      next if entry.name == '.' or entry.name == '..'
			
      child_path = File.join(path, entry.name)
			
      if self.stat!(child_path).directory?
        self.rm_r!(child_path) 
      else
        puts "remove! #{child_path}"
        self.remove!(child_path)
      end
    end

    puts "rmdir! #{path}"
    self.rmdir!(path)
  end
	
  def clear!(path)
    puts "clear! #{path}"
    self.dir.entries(path).each do |entry|
      self.rm_r!(File.join(path, entry.name))
    end
  end
end</code>
    <comment>I'm trying to clear a remote directory via sftp. I'm new to ruby and net-sftp so it wouldn't surprise me if there is a more elegant or built in way to do this. Also, it feels like there might be a faster way. I've extended the net::sftp::session in the following way. Thoughts?</comment>
    <created-at type="datetime">2009-08-19T15:38:24+00:00</created-at>
    <id type="integer">1008</id>
    <language>Ruby</language>
    <permalink>net-sftp-clear-a-remote-directory</permalink>
    <refactors-count type="integer">1</refactors-count>
    <title>NET::SFTP clear a remote directory</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-09-03T23:04:00+00:00</updated-at>
    <user-id type="integer">1662</user-id>
    <user>
      <id type="integer">1662</id>
      <identity-url>http://dane.oconnor.myopenid.com</identity-url>
      <name>dane.oconnor.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website></website>
    </user>
  </code>
  <code>
    <code>&lt;?php
function word_limiter( $text, $limit = 30, $chars = '0123456789' ) {
    if( strlen( $text ) &gt; $limit ) {
        $words = str_word_count( $text, 2, $chars );
        $words = array_reverse( $words, TRUE );
        foreach( $words as $length =&gt; $word ) {
            if( $length + strlen( $word ) &gt;= $limit ) {
                array_shift( $words );
            } else {
                break;
            }
        }
        $words = array_reverse( $words );
        $text = implode( " ", $words ) . '&amp;hellip;';
    }
    return $text;
}

$str = "Hello this is a list of words that is too long";
echo '1: ' . word_limiter( $str );
$str = "Hello this is a list of words";
echo '2: ' . word_limiter( $str );
?&gt;</code>
    <comment>It returns a string with a certain character limit, but still retaining whole words.
It breaks out of the foreach loop once it has found a string short enough to display, and the character list can be edited.

At the moment it reverses the array and then works through it, effectively going backwards, but would it be better to go forward?</comment>
    <created-at type="datetime">2009-07-30T09:37:03+00:00</created-at>
    <id type="integer">976</id>
    <language>PHP</language>
    <permalink>limit-amount-of-words-displayed</permalink>
    <refactors-count type="integer">6</refactors-count>
    <title>Limit amount of words displayed</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-12-08T23:41:18+00:00</updated-at>
    <user-id type="integer">1621</user-id>
    <user>
      <id type="integer">1621</id>
      <identity-url>http://charliefrancis.myopenid.com</identity-url>
      <name>charliefrancis</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website>www.team-space.co.uk</website>
    </user>
  </code>
  <code>
    <code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    /// &lt;summary&gt;
    /// Wraps an IEnumerable&amp;lt;T&amp;gt; and provides a thread-safe means of caching the values."/&gt;
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
    class ThreadSafeCachedEnumerable&lt;T&gt; : IEnumerable&lt;T&gt;
    {
        // An enumerator from the original IEnumerable&lt;T&gt;
        private IEnumerator&lt;T&gt; enumerator;

        // The items we have already cached (from this.enumerator)
        private IList&lt;T&gt; cachedItems = new List&lt;T&gt;();

        public ThreadSafeCachedEnumerable(IEnumerable&lt;T&gt; enumerable)
        {
            this.enumerator = enumerable.GetEnumerator();
        }

        #region IEnumerable&lt;T&gt; Members

        public IEnumerator&lt;T&gt; GetEnumerator()
        {
            // The index into the sequence
            int currentIndex = 0;

            // We will break with yield break 
            while (true)
            {
                // The currentIndex will never be decremented,
                // so we can check without locking first
                if (currentIndex &lt; this.cachedItems.Count)
                {
                    var current = this.cachedItems[currentIndex];
                    currentIndex += 1;
                    yield return current;
                }
                else
                {
                    // If !(currentIndex &lt; this.cachedItems.Count),
                    // we need to synchronize access to this.enumerator
                    lock (enumerator)
                    {
                        // See if we have more cached items ...
                        if (currentIndex &lt; this.cachedItems.Count)
                        {
                            var current = this.cachedItems[currentIndex];
                            currentIndex += 1;
                            yield return current;
                        }
                        else
                        {
                            // ... otherwise, we'll need to get the next item from this.enumerator.MoveNext()
                            if (this.enumerator.MoveNext())
                            {
                                // capture the current item and cache it, then increment the currentIndex
                                var current = this.enumerator.Current;
                                this.cachedItems.Add(current);
                                currentIndex += 1;
                                yield return current;
                            }
                            else
                            {
                                // We reached the end of the enumerator - we're done
                                yield break;
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
</code>
    <comment>Hello,

I created a this "ThreadSafeCachedEnumerable&lt;T&gt;" to speed up a few parts of my application.  The idea is to create cache items retrieved from an IEnumerable&lt;T&gt;.

Unfortunately, the code is getting a little ugly and the performance is hindered by the locking.  Any suggestions for making it easier to read or perform better would be appreciated.

Thanks,
-Charles</comment>
    <created-at type="datetime">2009-07-06T15:15:42+00:00</created-at>
    <id type="integer">945</id>
    <language>C#</language>
    <permalink>cached-ienumerable-t</permalink>
    <refactors-count type="integer">1</refactors-count>
    <title>Cached IEnumerable&lt;T&gt; </title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-07-08T02:52:45+00:00</updated-at>
    <user-id type="integer">1574</user-id>
    <user>
      <id type="integer">1574</id>
      <identity-url>http://charlesstrahan.pip.verisignlabs.com</identity-url>
      <name>Charles Strahan</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>class Array

   # insert i between each element in the array

   # this is O( 2n ) ( I think! Is it? ) as the length of the list is not needed, but we need to initially make a copy of the list
     def intersperse( v )
      tail = [ ].replace self # we need a shallow copy
      first_elem = tail.first
      second_elem = tail[ 1 ]
      arr = [ ]
      while first_elem
         arr.push first_elem
         if second_elem
            arr.push v
            tail = tail.drop 1
            first_elem = tail.first
            second_elem = tail[ 1 ]
         else
            break
         end
      end
      arr
   end

  # this is faster, but I worry that for large arrays, storing i in memory will makes this slower
  # Bearing that in mind, since the speed at which this goes is determined by the length of the list as well as the length of the list stored in memory ( i ) is O( n^2 ) ( again, please correct me )
  def intersperse( v )
     arr = [ ]
     i = 0
     j = 0
     inc_jay = lambda { j = j + 1 }
     while i &lt; length
        arr[ j ] = self[ i ]
        inc_jay.call
        if self[ i + 1 ]
           arr[ j ] = v
           inc_jay.call
        end
        i = i + 1
     end
     arr
  end
end</code>
    <comment>Intersperse is a method of the array class

Intersperse takes an object o, and returns the array ( self ), with o inserted between each element of self.</comment>
    <created-at type="datetime">2009-06-10T11:00:18+00:00</created-at>
    <id type="integer">907</id>
    <language>Ruby</language>
    <permalink>can-this-code-be-made-more-efficient</permalink>
    <refactors-count type="integer">4</refactors-count>
    <title>Ruby: array intersperse, efficiency</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-06-25T22:45:27+00:00</updated-at>
    <user-id type="integer">1544</user-id>
    <user>
      <id type="integer">1544</id>
      <identity-url>http://spoonerism.myopenid.com</identity-url>
      <name>spoonerism.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>#!/bin/bash

# Global variables
UPS='ups.domain.com'
FS='fs.domain.com'
FS_PORT='2'
PASSWORD='password'
MGR="/opt/bin/mgr"
MS_PORT='42'
CONSUMER_MS='msa1.domain.com msa2.domain.com msa3.domain.com'
BUSINESS_MS='msb1.domain.com msb2.domain.com msb3.domain.com'
count=0

echo "UID:EMAIL:ALIASES:COS:LAST_LOGIN:CREATED:DOMAIN:ENABLED:ANTISPAM:QUOTA_LIMIT:QUOTA_USED:FORWARDING_WITH_COPY\n" &gt; report.txt
for server in $CONSUMER_MS; do
        for uid in `$MGR -s $server -p $MS_PORT -w $PASSWORD USER mbox.domain.com LIST | awk '{ print $3 }'`; do
                # Query MS
                MS_OUT=`$MGR -s $server -p $MS_PORT -w $PASSWORD USER mbox.domain.com SHOW $uid | awk '{ print $2 " " $3 }'`
                used=` awk '/DISKUSAGE/ { print $2 }' &lt;&lt;&lt; "$MS_OUT"`
                limit=`awk '/BASEQUOTA/ { print $2 }' &lt;&lt;&lt; "$MS_OUT"`

                # Query FS
                FS_OUT=`$MGR -s $FS -p $FS_PORT -w $PASSWORD USER SHOW $uid mbox.domain.com`
                mailbox=`   awk '/MAILPATH/              {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                forwarding=`awk '/PSFORWARDASATTACHMENT/ {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                email=`     awk '/PSACCOUNTEMAILADDRESS/ {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                mailhost=`  awk '/MAILHOST/              {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                cos=`       awk '/MAILCLASS/             {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                enabled=`   awk '/ENABLEDFLAG/           {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
                last_login=`awk '/LASTSUCCESSFULLOGON/   {split($0,a,"="); print a[2]}' &lt;&lt;&lt; "$FS_OUT"`
			
				# Query mailbox
                if test -n "$mailhost" &amp;&amp; -n "$mailbox"
                then
                        echo "created=`ssh $mailhost -i mailbox_info.pub grep $mailbox UID | awk '{ print $2}'`"
                fi
                echo "$uid:$email:$aliases:$cos:$last_login:$created:$domain:$enabled:$antispam:$limit:$used:$forwarding" &gt;&gt; report.txt
                echo $((++count))
        done
done

for server in $BUSINESS_MS; do
        echo $server
done

echo 'Script completed.'</code>
    <comment>I'm trying to collect customer statistics with a shell script.Problem is that currently its runtime is about 50h which is too much. As I'm pretty new to scripting so my solution works but obviously it could be better.
My main concern currently is if the refactoring the if statements to use single awk instance would yield some speed gain.

After that I should continue to find some way to reduce those network calls and check if they could be replaced with ldap queries or something as I guess those would be faster to execute.</comment>
    <created-at type="datetime">2009-06-03T07:14:43+00:00</created-at>
    <id type="integer">894</id>
    <language>Bash</language>
    <permalink>adding-speed-to-script</permalink>
    <refactors-count type="integer">-37</refactors-count>
    <title>Adding speed to script</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-11-19T20:25:43+00:00</updated-at>
    <user-id type="integer">1532</user-id>
    <user>
      <id type="integer">1532</id>
      <identity-url>http://zmyrgel.blogspot.com</identity-url>
      <name>Zmyrgel</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website></website>
    </user>
  </code>
  <code>
    <code>    public static String findsite(InputStream text) {
      String site, line = null;
      Boolean nospace = true;

      BufferedReader reader = new BufferedReader(new InputStreamReader(text));

      try {
        line = reader.readLine();
      } catch(Exception e) {}

      if(line.indexOf("Full") == 0) {
        site = "fulltilt";
      } else if((line.indexOf("Pok") == 0) || (line.indexOf("POK") == 0)) {
        site = "pokerstars";
      } else if(line.indexOf("***** Hand History") == 0) {
        site = "888";
      } else if(line.indexOf("***") == 0) {
        site = "partypoker";
      } else if(line.indexOf("** Game ID") == 0) {
        site = "prima";
      } else if(line.indexOf("Everest") == 0) {
        site = "everest";
      } else if(line.indexOf("Betfair") == 0) {
        site = "betfair";
      } else if((line.indexOf("STAGE") == 0) || (line.indexOf("Stage") == 0)){
        site = "cereus";
     } else if((line.indexOf("Game #") == 0) || (line.indexOf("GAME #") == 0)) {
        site = "ipoker";
      } else if(line.indexOf("** Hand #") == 0) {
        site = "ladbrokes";
      } else if(line.indexOf("WPEX") == 0) {
        site = "wpex";
      } else if(line.indexOf("Hand Start.") == 0) {
        site = "tribeca";
      } else if(line.indexOf("&lt;game id") == 0) {
        site = "carbon";
      } else if(line.indexOf("Bodog") == 0) {
        site = "bodog";
      } else if(line.indexOf("Hand") == 0) {
        site = "cake";
      } else {
        site = "fulltilt";
      }
      return site;
    }

    public void txtToHand(InputStream sin) {
        String site;
        String tourn = "false";

        //remove any blanklines and such
        sin = striphistory(sin);
        sin.mark(0);

        site = findsite(sin);
        try {
          sin.reset();
        } catch(Exception e) {}

        try {
        // Create an input character stream from standard in
        ANTLRInputStream input = new ANTLRInputStream(sin);
        
          if( site == "fulltilt") {
              FullTiltLexer lexer = new FullTiltLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              FullTiltParser parser = new FullTiltParser(tokens);

              parser.hand_history();
              if(parser.hd.seats.isEmpty()) {
                throw new NoSeatsException();
              }

              parser.hd.site = "fulltilt";
              parser.hd.site_id = 1;
              hand = parser.hd;

          } else if (site == "pokerstars") {
              PokerStarsLexer lexer = new PokerStarsLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              PokerStarsParser parser = new PokerStarsParser(tokens);
  
              parser.hand_history();
    
              parser.hd.site = "pokerstars";
              parser.hd.site_id = 2;
              hand = parser.hd;

          } else if (site == "partypoker") {
              PartyPokerLexer lexer = new PartyPokerLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              PartyPokerParser parser = new PartyPokerParser(tokens);
  
              parser.hand_history();

              parser.hd.site = "partypoker";
              parser.hd.site_id = 3;
              hand = parser.hd;

          } else if (site == "cereus") {
              CereusLexer lexer = new CereusLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              CereusParser parser = new CereusParser(tokens);
  
              parser.hand_history();

              parser.hd.site = "cereus";
              parser.hd.site_id = 4;
              hand = parser.hd;

          } else if (site == "cake") {
              CakeLexer lexer = new CakeLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              CakeParser parser = new CakeParser(tokens);
  
              parser.hand_history();
      
              parser.hd.site = "cake";
              parser.hd.site_id = 5;
              hand = parser.hd;

          } else if (site == "prima") {
              PrimaLexer lexer = new PrimaLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              PrimaParser parser = new PrimaParser(tokens);
  
              parser.hand_history();
        
              parser.hd.site = "prima";
              parser.hd.site_id = 6;
              hand = parser.hd;

          } else if (site == "ipoker") {
              IPokerLexer lexer = new IPokerLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              IPokerParser parser = new IPokerParser(tokens);
  
              parser.hand_history();
    
              parser.hd.site = "ipoker";
              parser.hd.site_id = 7;
              hand = parser.hd;

          } else if (site == "888") {
              EightLexer lexer = new EightLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lexer);
              EightParser parser = new EightParser(tokens);
  
              parser.hand_history();
    
              parser.hd.site = "888";
              parser.hd.site_id = 7;
              hand = parser.hd;

          }
      
      } catch(Exception e) {}

    }
</code>
    <comment>I don't know too much about java but this problem is not really language specific -- it just looks horrible..
Basically we have 6-8 grammars that get loaded depending on what the first line or so of a file looks like.
So not only do we match that first line but then we have to come back through and match the site name just
to load 1 of 8 different grammars.

There are two functions here (hope I didn't cut anything important out).
  findsite basically determines from the first line what grammar we are going to deal with..
  
  txtToHand actually loads our grammar

 -- perhaps you can make it cleaner, more succint and even speed it up? -- speed is really the most important feature I want in this refactor</comment>
    <created-at type="datetime">2009-05-21T16:01:19+00:00</created-at>
    <id type="integer">880</id>
    <language>Java</language>
    <permalink>speed-up-this-horrendous-conditional-chain</permalink>
    <refactors-count type="integer">4</refactors-count>
    <title>speed up this horrendous conditional chain</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-08-25T03:28:13+00:00</updated-at>
    <user-id type="integer">1338</user-id>
    <user>
      <id type="integer">1338</id>
      <identity-url>http://feydr.myopenid.com</identity-url>
      <name>feydr.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website></website>
    </user>
  </code>
  <code>
    <code>class Item
  def raw_tag_list; "one, two, three, four, five"; end
  def public_tags; [&lt;#Tag name=two&gt;, &lt;#Tag name=five&gt;, &lt;#Tag name=one&gt;, &lt;#Tag name=four&gt;, &lt;#Tag name=three&gt;]; end

  def tags
    return Array.new if raw_tag_list.nil?

    # Get the raw tag list, split, squish (removed whitespace), and add each to raw_tag_array
    # Make sure we skip if the array already has that tag name (remove any duplicates that occur)
    raw_tag_array = Array.new
    raw_tag_list.split(',').each do |raw_tag|
      next if raw_tag_array.include?(raw_tag.squish)
      raw_tag_array &lt;&lt; raw_tag.squish
    end

    tags = Array.new

    # grab all the tag objects
    tags_out_of_order = public_tags
    if tags_out_of_order.size &gt; 0
      # resort them to match raw_tag_list order
      raw_tag_array.each do |tag_name|
        tag = tags_out_of_order.select { |tag| tag.name == tag_name }
        tags &lt;&lt; tag
      end
      # at this point, we have an array, with arrays of object  [[tag], [tag], [tag]]
      # use compact to remove any nil objects, and flatten to convert it to [tag, tag, tag]
      tags = tags.compact.flatten
    end

    tags
  end
end</code>
    <comment>Is there a faster way to do this? I need to sort an array of tags according to the order of tags in a string. At the moment, I make the array then select as go through. But this is slow. Is there a fancy ruby alternative?</comment>
    <created-at type="datetime">2009-03-30T22:48:23+00:00</created-at>
    <id type="integer">807</id>
    <language>Ruby</language>
    <permalink>tag-ordering-code</permalink>
    <refactors-count type="integer">12</refactors-count>
    <title>Tag ordering code</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-04-01T17:47:43+00:00</updated-at>
    <user-id type="integer">1425</user-id>
    <user>
      <id type="integer">1425</id>
      <identity-url>http://k776.blogspot.com</identity-url>
      <name>k776</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">3</refactors-count>
      <website>http://k776.blogspot.com</website>
    </user>
  </code>
  <code>
    <code>def haar_1d(a)
	r = []
	(Math.log(a.length) / Math.log(2) - 1).to_i.downto(0) { |j|
		as = []
		ad = []
		(1 &lt;&lt; j).times { |k|
			u = 2 * k
			au = a[u] * 0.5
			av = a[u + 1] * 0.5
			as &lt;&lt; au + av
			ad &lt;&lt; au - av
		}
		a = as
		r = ad + r
	}
	a + r
end</code>
    <comment>Anyone want to help me optimize this algorithm for speed? I know that C would probably be the quickest, but I wanted to try something easy while I'm learning about image processing. This code is as far as I got without major logic restructuring. Input is a one-dimensional array of 2^n length.

Edit: L's looked too similar to 1's.</comment>
    <created-at type="datetime">2009-01-26T03:33:55+00:00</created-at>
    <id type="integer">719</id>
    <language>Ruby</language>
    <permalink>fast-haar-wavelet-transform</permalink>
    <refactors-count type="integer">4</refactors-count>
    <title>Fast Haar Wavelet Transform</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-01-30T22:21:47+00:00</updated-at>
    <user-id type="integer">1291</user-id>
    <user>
      <id type="integer">1291</id>
      <identity-url>http://data.map.myopenid.com</identity-url>
      <name>data.map.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>public static String[] loadFile(final String f) {
		String thisLine;
		final SortedSet&lt;String&gt; s = new TreeSet&lt;String&gt;();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(f));
			while ((thisLine = br.readLine()) != null)
				s.add(thisLine.trim());
		} catch (IOException ioe) {
			System.err.println("Error reading file " + f);
			throw new RuntimeException(ioe);
		} finally {
			if(br!=null) try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return s.toArray(new String[s.size()]);
	}</code>
    <comment>Has full "finally" blocks, passes findBugs check, should be speedy using TreeSet.  Useful for loading data files.</comment>
    <created-at type="datetime">2008-11-25T07:04:17+00:00</created-at>
    <id type="integer">620</id>
    <language>Java</language>
    <permalink>efficiently-load-a-text-file-into-a-sorted-string-array</permalink>
    <refactors-count type="integer">1</refactors-count>
    <title>Efficiently load a text file into a sorted String Array</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-11-25T08:18:54+00:00</updated-at>
    <user-id type="integer">188</user-id>
    <user>
      <id type="integer">188</id>
      <identity-url>http://firesalamander.myopenid.com</identity-url>
      <name>firesalamander.myopenid.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>// For faster converting of dates to strings
char* sLeadingZeroIntegerValues[] = {
"00","01","02","03","04","05","06","07","08","09",
"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","58","59"};

// For faster converting of dates to strings
char date_buf[20] = "XXXX-XX-XX XX:XX:XX";

// Just some non printable ascii chars, for delimiters
const string DataTable::SECTION_DELIMITER = "\1";
const string DataTable::ROW_DELIMITER = "\2";
const string DataTable::COL_DELIMITER = "\3";
const string DataTable::TABLE_DELIMITER = "\4";
const string DataTable::NAME_VALUE_DELIMITER = "\5";

void CDbInterface::LoadRecordsetIntoStreamAsBigString(
	_RecordsetPtr&amp; pRs,  ostringstream&amp; ostrm)
{



		DataTable::TypeList types; 

		if (!pRs-&gt;EndOfFile)
		{
			int nColumns = pRs-&gt;Fields-&gt;GetCount();

			// Column names
			for (long i = 0L; i &lt; nColumns; i++)
			{
				string name = (const char*) (_bstr_t) pRs-&gt;Fields-&gt;GetItem(i)-&gt;Name;
				if (i != 0) ostrm &lt;&lt; DataTable::COL_DELIMITER;
				ostrm &lt;&lt; name.c_str();
			}

			ostrm &lt;&lt; DataTable::SECTION_DELIMITER;

			// Column types
			for (long i = 0L; i &lt; nColumns; i++)
			{
				ADODB::DataTypeEnum dbType = pRs-&gt;Fields-&gt;GetItem(i)-&gt;GetType();
				if (i != 0) ostrm &lt;&lt; DataTable::COL_DELIMITER;

				DataTableDataType type;

				if (dbType == ADODB::DataTypeEnum::adVarWChar
					|| dbType == ADODB::DataTypeEnum::adWChar)
				{
					type = DataTableDataType::String;
				}
				else if (dbType == ADODB::DataTypeEnum::adInteger)
				{
					type = DataTableDataType::Integer;
				}
				else if (dbType == ADODB::DataTypeEnum::adUnsignedTinyInt)
				{
					type = DataTableDataType::Bool;
				}
				else if (dbType == ADODB::DataTypeEnum::adDate)
				{
					type = DataTableDataType::Date;
				}

				ostrm &lt;&lt; type;
				types.push_back(type);
			}

			ostrm &lt;&lt; DataTable::SECTION_DELIMITER;

			// Load the rows and columns
			int nRow = 0;
			ADODB::FieldsPtr pFields = pRs-&gt;Fields;
			char buf[80];
			::SYSTEMTIME sysTime;
			_variant_t var;
			char* pBstr;
			long nBstrLen;

			DWORD time1 = ::GetTickCount();

			while(!pRs-&gt;EndOfFile)
			{
				if (nRow != 0) 
                                    ostrm &lt;&lt; DataTable::ROW_DELIMITER;

				for (long i = 0L; i &lt; nColumns; i++)
				{

// For every column in every row...  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;   START HERE
// Can we make this even faster?

					if (i != 0)
                                             ostrm &lt;&lt; DataTable::COL_DELIMITER;


					var = pFields-&gt;GetItem(i)-&gt;GetValue();

					if (V_VT(&amp;var) == VT_BSTR)
					{
						// Copy every other byte of the bstr into the stream.
						// That's doing an ansi conversion w/o allocating unnecessary memory
						pBstr = (char*) var.bstrVal;
						nBstrLen = *(pBstr-4);
						for (int i = 0; i &lt; nBstrLen; i+=2)
						{
							ostrm &lt;&lt; (char) pBstr[i];	
						}
					}
					else if (V_VT(&amp;var) == VT_I4
					|| V_VT(&amp;var) == VT_UI1
					|| V_VT(&amp;var) == VT_I2
					|| V_VT(&amp;var) == VT_BOOL)
					{
						ostrm &lt;&lt; itoa(((int)var),buf,10);
					}
					else if (V_VT(&amp;var) == VT_DATE)
					{
						::VariantTimeToSystemTime(var,&amp;sysTime);
						memcpy(date_buf,itoa(sysTime.wYear,buf,10),4);
						memcpy(date_buf+5,sLeadingZeroIntegerValues[sysTime.wMonth],2);
						memcpy(date_buf+8,sLeadingZeroIntegerValues[sysTime.wDay],2);
						memcpy(date_buf+11,sLeadingZeroIntegerValues[sysTime.wHour],2);
						memcpy(date_buf+14,sLeadingZeroIntegerValues[sysTime.wMinute],2);
						memcpy(date_buf+17,sLeadingZeroIntegerValues[sysTime.wSecond],2);
						ostrm &lt;&lt; date_buf;
					}

// End of the part we're trying to make faster...   &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;  END HERE

					else if (V_VT(&amp;var) == VT_EMPTY
						|| V_VT(&amp;var) == VT_NULL)
					{
						if (types[i] == DataTableDataType::String)
						{
							ostrm &lt;&lt; "";
						}
						else if (types[i] == DataTableDataType::Integer)
						{
							ostrm &lt;&lt; DataTableNullInteger;
						}
						else if (types[i] == DataTableDataType::Bool)
						{
							ostrm &lt;&lt; "0";
						}
						else if (types[i] == DataTableDataType::Date)
						{
							ostrm &lt;&lt; "";
						}
					}

				}
				pRs-&gt;MoveNext();
				nRow++;
			}
		}


                ostrm &lt;&lt; DataTable::FINAL_DELIMITER;
}
</code>
    <comment>Can we make the code between "START HERE" and "END HERE" faster?   The code is iterating thru the rows and columns of an ADODB Recordset, using Microsoft's generated wrapper classes.   I'm streaming it into a buffer as a big flattened string.   Can we make the flattening go faster?   Notice there is already a trick for turning the BSTRs into ANSI strings (I'm only dealing with ASCII chars).   Anything else?</comment>
    <created-at type="datetime">2008-11-11T12:30:41+00:00</created-at>
    <id type="integer">599</id>
    <language>C++</language>
    <permalink>stream-adodb-recordset-into-a-buffer</permalink>
    <refactors-count type="integer">2</refactors-count>
    <title>Stream ADODB recordset into a buffer</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-02-15T14:43:33+00:00</updated-at>
    <user-id type="integer">1155</user-id>
    <user>
      <id type="integer">1155</id>
      <identity-url>http://ctrager.blogspot.com</identity-url>
      <name>ctrager.blogspot.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website>http://ifdefined.com</website>
    </user>
  </code>
  <code>
    <code>#!/usr/bin/ruby
require 'builder'
require 'fasterCSV'
require 'postgres'

file = File.new("testing.xml", "w")
xml = Builder::XmlMarkup.new( :target =&gt; file, :indent =&gt; 4 )
xml.instruct! :xml, :version=&gt;"1.0", :encoding=&gt;"UTF-8", :standalone=&gt;"no"
xml.declare! :DOCTYPE, :questestinterop, :SYSTEM, "ims_qtiasiv1p2.dtd"
pghost =  'localhost'
pgport = 5432
pgtbl = 'tweek'
dbname = 'postgres'

file.print(xml.target!)

## INPUT PostgreSQL ##
# Loop for each record
##QUERY TIME##
(7493..7494).each do |i|
  #query = 'SELECT question_stem, option_text, author_id, module_code, correct, weighting, option_id, feedback FROM RESULTS_JOIN'
  query_aqo = 'SELECT * FROM assessment_question_option_stat AQO WHERE AQO.question_id = ' + i.to_s
  query_q = 'SELECT * FROM question Q WHERE Q.question_id = ' + i.to_s
  query_qn = 'SELECT * FROM question_module QM WHERE QM.question_id = ' + i.to_s
  query_qo = 'SELECT * FROM question_option QO WHERE QO.question_id = ' + i.to_s

  db = PGconn.connect(pghost, pgport,'','',pgtbl,dbname,'')
  ##RESULTS##
  res_aqo = db.exec(query_aqo)
  res_q = db.exec(query_q)
  res_qn = db.exec(query_qn)
  res_qo = db.exec(query_qo)

  ##ASSIGN PG RESULTS##
 
  # Item title / Module code
  item_title = res_qn[0][1]
  module_code = item_title
  # Item ident / exam title

  # Question
  question_text = res_q[0][1]

  # Paper name
  item_ident ="NSFB_electronics_01_v1p2"
  
  # Get number of questions and their answers and add them to their arrays.
  response_ident = Array.new()
  response_mattext = Array.new()
  response_boolean_tf = Array.new()
  response_feedback = Array.new()
  res_qo.to_a.each do |row|
     #Question Numbers
     response_ident.push(row[1].to_s)
     #Answers
     response_mattext.push(row[2].to_s)
     # Answers true/false to 1/0
     if (row[3].to_s == "t")
       response_boolean_tf.push("1")
     else
       response_boolean_tf.push("0")
     end   
     # Go get some responses to these questions
     response_feedback.push(row[6].to_s)
  end

  ## TBC first 10 words for the title ##
  num_options = res_qo.to_a.length
  rcardinality = "single"
  rtiming = "No"
  shuffle = "Yes"

  ##OUTPUT ##
  # Start the XML fun
  # Question the participant!
  xml.questestinterop do
    xml.item(:title =&gt; item_title, :ident =&gt; item_ident) do
      # Presentation
      xml.presentation do
        xml.material do
          xml.mattext(question_text)
        end
        xml.response_lid(:ident =&gt; "all filer", :rcardinality =&gt; rcardinality, :rtiming =&gt; rtiming) do
          xml.render_choice(:shuffle =&gt; shuffle) do
            for j in (0..num_options -1)
              xml.response_label(:ident =&gt; response_ident[j])do
                xml.material do
                    xml.mattext(response_mattext[j])                   
                end
              end
            end
          end
        end
      end

      ## Response Processing ##
      #find number of answers and repeat
      #use feedback numbers to have the number of questions (they're going to be the same aren't they?
      rfeedback = response_feedback.length
      xml.resprocessing do
        xml.outcomes do
          xml.decvar()
        end
        for k in (0..rfeedback -1)
          xml.respcondition(:title =&gt; response_mattext[k], :continue =&gt; "Yes") do
            xml.conditionvar do
              xml.varequal( response_ident[k], :respident =&gt; module_code)
            end
          xml.setvar(response_boolean_tf[k], :action =&gt; "Set")
          xml.displayfeedback(:feedbacktype =&gt; "Response", :linkrefid =&gt; "")
          end
        end
      end

      ## FEEDBACK ##
      #find the number of answers and repeat      
      for l in (0..rfeedback - 1)
        xml.itemfeedback(:ident =&gt; response_mattext[l], :view =&gt; "Candidate") do
          xml.material do
              xml.mattext(response_feedback[l])
          end
        end
      end
    end
  end
end
file.close</code>
    <comment>Is it possible to speed up this script? I've got about 5,000 records to go through and the first 1,000 took just over a minute, which I was quite impressed by, but speeding it up might show the other people in the office how good Ruby is.
Cheers,
Dan</comment>
    <created-at type="datetime">2008-10-27T12:14:21+00:00</created-at>
    <id type="integer">564</id>
    <language>Ruby</language>
    <permalink>speed-up-db-access</permalink>
    <refactors-count type="integer">0</refactors-count>
    <title>Speed up DB access?</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-10-27T12:14:21+00:00</updated-at>
    <user-id type="integer">1126</user-id>
    <user>
      <id type="integer">1126</id>
      <identity-url>http://danw85.myopenid.com</identity-url>
      <name>Dan</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website>dan-webb.co.uk</website>
    </user>
  </code>
  <code>
    <code>function state_classes(updateall) {
    var value = display.value * 1, tempcolor;
    for (var i = 1; i &lt;= 118; i++) {
        tempcolor = updateall;
        if (!boil[i] &amp;&amp; !melt[i]) {
            if (element_ids[i].state !== "Unknown") {
                tempcolor = true;
                element_ids[i].state = "Unknown";
            }
        } else if (value &gt; boil[i]) {
            if (element_ids[i].state !== "Gas") {
                tempcolor = true;
                element_ids[i].state = "Gas";
            }
        } else if (value &gt; melt[i]) {
            if (element_ids[i].state !== "Liquid") {
                tempcolor = true;
                element_ids[i].state = "Liquid";
            }
        } else if (element_ids[i].state !== "Solid") {
            tempcolor = true;
            element_ids[i].state = "Solid";
        }
        if (tempcolor) {
            if (wholetable.inverted)    element_ids[i].style.backgroundColor = statecolors[1][element_ids[i].state];
            else                        element_ids[i].style.color = statecolors[0][element_ids[i].state];
        }
    }
}</code>
    <comment>Logic is cheap and DOM is expensive, so I set an element's current state in a variable and only change the color if its state has changed. The code seems much longer than necessary, though. Passing in true to updateall recolors everything, a rare occurence, otherwise 0 is passed in.</comment>
    <created-at type="datetime">2008-09-15T17:09:57+00:00</created-at>
    <id type="integer">492</id>
    <language>JavaScript</language>
    <permalink>color-elements-by-state-of-matter-at-temperature</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Color elements by state of matter at temperature</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-09-18T04:47:25+00:00</updated-at>
    <user-id type="integer">526</user-id>
    <user>
      <id type="integer">526</id>
      <identity-url>http://lucent.livejournal.com</identity-url>
      <name>Lucent</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website>http://www.ptable.com/</website>
    </user>
  </code>
  <code>
    <code>module Enumerable
  # Sorts the enumeration by mapping the
  # values in the enumeration through the given block.
  #
  # The method takes a tuple of Symbols which indicate the sort direction
  # for each value returned by the block.
  #
  # The block must return a tuple of the same size as the direction tuple.
  def sort_by_multiple(*directions, &amp;block)
    # Performance optimization
    if directions.all? { |d| d == :ascending || d == :asc }
      return self.sort_by(&amp;block)
    end
    sorted = ((self.collect do |obj|
      [block.call(obj), obj]
    end).sort do |a, b|
      if !a[0].kind_of?(Array) || !b[0].kind_of?(Array)
        raise TypeError,
          "The block must return values of type Array."
      elsif a[0].size != directions.size || b[0].size != directions.size
        raise ArgumentError,
          "Size mismatch between block return value and " +
          "directions parameter: " +
          "#{a[0].inspect}, #{b[0].inspect}, #{directions.inspect}"
      else
        result = 0
        directions.each_with_index do |direction, index|
          value_a = a[0][index]
          value_b = b[0][index]
          if direction == :descending || direction == :desc
            result = value_b &lt;=&gt; value_a
          elsif direction == :ascending || direction == :asc
            result = value_a &lt;=&gt; value_b
          else
            raise ArgumentError,
              "Direction must be one of: :ascending, :descending, " +
              "was: #{direction.inspect}."
          end
          break if result != 0
        end
        result
      end
    end).collect do |pair|
      pair[1]
    end
    sorted
  end
end
</code>
    <comment>require "benchmark"
data_set = []
10000.times do
  data_set &lt;&lt; [rand(10), rand(10), rand(10)]
end
Benchmark.measure do
  data_set.sort_by_multiple(:ascending, :descending, :ascending) { |o| o }
end
# =&gt; #&lt;Benchmark::Tms:0x2135ce8 @cutime=0.0, @label="", @stime=0.0, @real=1.84591794013977, @utime=1.81, @total=1.81, @cstime=0.0&gt;
Benchmark.measure do
  data_set.sort_by_multiple(:ascending, :ascending, :ascending) { |o| o }
end
# =&gt; #&lt;Benchmark::Tms:0x207cab8 @cutime=0.0, @label="", @stime=0.0, @real=0.0400059223175049, @utime=0.0399999999999996, @total=0.0399999999999996, @cstime=0.0&gt;

Obviously, there's a lot of potential for optimizing this method.  Suggestions?</comment>
    <created-at type="datetime">2008-09-05T13:11:27+00:00</created-at>
    <id type="integer">482</id>
    <language>Ruby</language>
    <permalink>sort_by_multiple</permalink>
    <refactors-count type="integer">12</refactors-count>
    <title>sort_by_multiple</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-09-05T19:25:33+00:00</updated-at>
    <user-id type="integer">994</user-id>
    <user>
      <id type="integer">994</id>
      <identity-url>http://sporkmonger.com/</identity-url>
      <name>Sporkmonger</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">9</refactors-count>
      <website>http://sporkmonger.com/</website>
    </user>
  </code>
  <code>
    <code>## pitimes.c
#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

#define ITERS 10000000
#define TESTWITH(x) {                                                       \
    diff = 0.0;                                                             \
    time1 = clock();                                                        \
    for (i = 0; i &lt; ITERS; ++i)                                             \
        diff += (x) - M_PI;                                                 \
    time2 = clock();                                                        \
    printf("%s\t=&gt; %e, time =&gt; %f\n", #x, diff, diffclock(time2, time1));   \
}

static inline double
diffclock(clock_t time1, clock_t time0)
{
    return (double) (time1 - time0) / CLOCKS_PER_SEC;
}

int
main()
{
    int i;
    clock_t time1, time2;
    double diff;

    /* Warmup. The atan2 case catches GCC's atan folding (which would
     * optimise the ``4 * atan(1) - M_PI'' to a no-op), if -fno-builtin
     * is not used. */
    TESTWITH(4 * atan(1))
    TESTWITH(4 * atan2(1, 1))

#if defined(__GNUC__) &amp;&amp; (defined(__i386__) || defined(__amd64__))
    extern double fldpi();
    TESTWITH(fldpi())
#endif

    /* Actual tests start here. */
    TESTWITH(atan2(0, -1))
    TESTWITH(acos(-1))
    TESTWITH(2 * asin(1))
    TESTWITH(4 * atan2(1, 1))
    TESTWITH(4 * atan(1))

    return 0;
}
## fldpi.c
double
fldpi()
{
    double pi;
    asm("fldpi" : "=t" (pi));
    return pi;
}
## build.sh [shell-unix-generic]
#!/bin/sh
gcc -O3 -Wall -c           -m32 -o fldpi-32.o fldpi.c
gcc -O3 -Wall -c           -m64 -o fldpi-64.o fldpi.c

gcc -O3 -Wall -ffast-math  -m32 -o pitimes1-32 pitimes.c fldpi-32.o
gcc -O3 -Wall              -m32 -o pitimes2-32 pitimes.c fldpi-32.o -lm
gcc -O3 -Wall -fno-builtin -m32 -o pitimes3-32 pitimes.c fldpi-32.o -lm
gcc -O3 -Wall -ffast-math  -m64 -o pitimes1-64 pitimes.c fldpi-64.o -lm
gcc -O3 -Wall              -m64 -o pitimes2-64 pitimes.c fldpi-64.o -lm
gcc -O3 -Wall -fno-builtin -m64 -o pitimes3-64 pitimes.c fldpi-64.o -lm
</code>
    <comment>Solutions welcome in any language. :-) I'm looking for the fastest way to obtain the value of pi, as a personal challenge. More specifically I'm using ways that don't involve using #defined constants like M_PI, or hardcoding the number in.

The program below tests the various ways I know of. The inline assembly version is, in theory, the fastest option, though clearly not portable; I've included it as a baseline to compare the other versions against. In my tests, with builtins, the 4 * atan(1) version is fastest on GCC 4.2, because it auto-folds the atan(1) into a constant. With -fno-builtin specified, the atan2(0, -1) version is fastest.

Apart from testing between various compiler flags (I've compared 32-bit against 64-bit too, because the optimisations are different), I've also tried switching the order of the tests around. The atan2(0, -1) version still comes out top every time, though.

I'm keen to hear what results you have, as well as improvements to the testing process. :-)

ETA: I noticed that the inline assembly was treated as a constant and hoisted out of the loop. Thus, I've included the fldpi in a separate file, so that it gets treated like a function call. As a bonus, now the frame setup times get counted too, just like all the other function calls.</comment>
    <created-at type="datetime">2008-07-30T03:50:29+00:00</created-at>
    <id type="integer">405</id>
    <language>C</language>
    <permalink>fastest-way-to-get-value-of-pi</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Fastest way to get value of pi</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-09-02T01:31:50+00:00</updated-at>
    <user-id type="integer">611</user-id>
    <user>
      <id type="integer">611</id>
      <identity-url>http://chris.jester-young.name/</identity-url>
      <name>Chris Jester-Young</name>
      <rating type="float">4.3333</rating>
      <refactors-count type="integer">29</refactors-count>
      <website></website>
    </user>
  </code>
  <code>
    <code>public static function getColorFromString(color:String):int
{
    return parseInt(new RegExp(/[0-9a-fA-F]+/).exec(color),16);
}
</code>
    <comment>Script was written on AS3. Translated parametr like "#ff6766". Need to get 0xff6766. I think RegExp slow for this purposes.</comment>
    <created-at type="datetime">2008-07-11T10:54:20+00:00</created-at>
    <id type="integer">361</id>
    <language>ActionScript</language>
    <permalink>get-color-from-string</permalink>
    <refactors-count type="integer">7</refactors-count>
    <title>Get color from string</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-12-31T14:50:46+00:00</updated-at>
    <user-id type="integer">807</user-id>
    <user>
      <id type="integer">807</id>
      <identity-url>http://shaman4d.blogspot.com</identity-url>
      <name>shaman4d.blogspot.com</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website></website>
    </user>
  </code>
  <code>
    <code>$.fn.textNodes = function() {
  var ret = [];
  this.each( function() {
    var fn = arguments.callee;
    $(this).contents().each( function() {
      if ( this.nodeType == 3 || $.nodeName(this, "br") ) 
        ret.push( this );
      else fn.apply( $(this) );
    });
  });
  return $(ret);
}

// example: wrap all text nodes in a span
$("body").textNodes().wrap("&lt;span/&gt;");</code>
    <comment>This function returns a jQuery object containing all descendent text nodes (and &lt;br/&gt; line breaks) within the context node. I'm looking to make this function as fast as possible, since I need to call it very frequently.</comment>
    <created-at type="datetime">2008-06-29T07:35:02+00:00</created-at>
    <id type="integer">341</id>
    <language>JavaScript</language>
    <permalink>jquery-all-descendent-text-nodes-within-a-node</permalink>
    <refactors-count type="integer">7</refactors-count>
    <title>[jQuery] All descendent text nodes within a node</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2009-03-10T17:41:04+00:00</updated-at>
    <user-id type="integer">724</user-id>
    <user>
      <id type="integer">724</id>
      <identity-url>http://tra.nslator.jp</identity-url>
      <name>jed</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website>http://tra.nslator.jp</website>
    </user>
  </code>
  <code>
    <code>function calc_color(value, start, end, min, max) {
    var n = (value - min) / (max - min);
    var s = parseInt(start.replace("#", ""), 16);
    var e = parseInt(end.replace("#", ""), 16);

    var r = Math.round(((e &gt;&gt; 16) - (s &gt;&gt; 16)) * n) + (s &gt;&gt; 16);
    var g = Math.round((((e &gt;&gt; 8) &amp; 0xFF) - ((s &gt;&gt; 8) &amp; 0xFF)) * n) + ((s &gt;&gt; 8) &amp; 0xFF);
    var b = Math.round(((e &amp; 0xFF) - (s &amp; 0xFF)) * n) + (s &amp; 0xFF);
    b |= (r &lt;&lt; 16) | (g &lt;&lt; 8);

    return "#" + ("000000" + b.toString(16)).slice(-6);
}</code>
    <comment>Watch out for edge cases, like calculating and returning a single 0 when 00 is necessary. A lot more interested in speed than simplicity, obviously.</comment>
    <created-at type="datetime">2008-03-11T08:27:22+00:00</created-at>
    <id type="integer">254</id>
    <language>JavaScript</language>
    <permalink>hex-color-between-two-colors</permalink>
    <refactors-count type="integer">3</refactors-count>
    <title>Hex color between two colors</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-05-06T10:28:37+00:00</updated-at>
    <user-id type="integer">526</user-id>
    <user>
      <id type="integer">526</id>
      <identity-url>http://lucent.livejournal.com</identity-url>
      <name>Lucent</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website>http://www.ptable.com/</website>
    </user>
  </code>
  <code>
    <code># Boids - A Shoes Application
#
# Author       : Wally Glutton - http://stungeye.com
# Summary      : A hungry swarm indeed!
#
# Notes        : My home-rolled Vector class appears to be quicker than the matrix library Vectors.
# Boid Algo    : http://www.vergenet.net/~conrad/boids/pseudocode.html
#
# Required     : You must have Shoes installed to view the boids.
# Get Shoes    : http://code.whytheluckystiff.net/shoes/
# Learn Shoes  : http://hackety.org/press/nks.html
#
# Code License : http://creativecommons.org/licenses/by-sa/2.5/ca/


srand
NUM_BOIDS = 30
NUM_FOODSTUFF = 6
boids = []
foodstuff = []

Shoes.app do
    stroke rgb(0x30, 0x30, 0x05, 0.5)
    app = self
    NUM_BOIDS.times { |i| boids[i] = Boid.new rand * self.width, rand * self.height, random(-5, 5), random(-5, 5) }
    NUM_FOODSTUFF.times { |i| foodstuff[i] = Food.new app}
    animate(24) do
        clear do
            boids.each do |boid|
                boid.calculate_avoidance_delta boids   # Avoid other boids
                boid.calculate_attraction_delta boids  # Gravitate towards the centre-of-mass of nearby boids
                boid.calculate_allignment_delta boids  # Allign velocity with nearby boids
                boid.calculate_hunting_delta foodstuff # Be on the lookout for food
                boid.calculate_stay_visible_delta self # Don't fly too far from home
                
                boid.apply_deltas
                boid.limit_speed
                
                boid.move
                boid.draw self, app
                
                foodstuff.each do |food|
                    food.eaten? boid
                end
            end
            foodstuff.each do |food|
                food.draw self
            end
        end
    end
end

class Food
    RADIUS = 30
    attr_reader :position
    def initialize app
        @app = app
        spawn
    end
    def spawn
        @size = RADIUS
        @position = VectorK.new rand * @app.width, rand * @app.height
    end
    def eaten? boid
        if @position.nearby? RADIUS, boid.position
            @size -= 1
        end
        if @size &lt; 0
            spawn
        end
    end
    def draw slot
        @app.fill rgb(0xFF, 0x30, 0xFF, 0.4)
        slot.oval :left =&gt; @position.x, :top =&gt; @position.y, :radius =&gt; @size, :center =&gt; true
    end
end

class Boid
    RADIUS = 20
    MAX_SPEED = 25
    AVOID_RADIUS = RADIUS*3 # Avoid other boids within this radius
    AVOID_DAMPER = 100
    ATTRACTION_RADIUS = RADIUS*8 # Gravitate to the centre of mass of boids within this radius
    ATTRACTION_DAMPER = 30
    ALLIGNMENT_RADIUS = RADIUS*3 # Allign velocity with boids within this radius
    ALLIGNMENT_DAMPER =  30
    HUNTING_RADIUS = RADIUS*5 # Locate food within this radius
    HUNTUNG_DAMPER = 10
    STAY_VISIBLE_DAMPER = 300
    
    attr_reader :velocity, :position
    
    def initialize x, y, vx, vy
        @velocity = VectorK.new vx, vy 
        @position = VectorK.new x, y
        @velocity_delta = VectorK.new 0, 0
    end
    def calculate_avoidance_delta boids
        boids.each do |other|
            if @position.nearby? AVOID_RADIUS, other.position
                @velocity_delta += (@position - other.position) / AVOID_DAMPER
            end
        end
    end
    def calculate_attraction_delta boids
        average_position = VectorK.new 0, 0
        visible_boids = 0
        boids.each do |other|
            if @position.nearby? ATTRACTION_RADIUS, other.position
                average_position += other.position
                visible_boids += 1
            end
        end
        average_position /= visible_boids
        @velocity_delta +=  (average_position - @position) / ATTRACTION_DAMPER
    end
    def calculate_allignment_delta boids
        allignment_delta = VectorK.new 0, 0
        visible_boids = 0
        boids.each do |other|
            if @position.nearby? ALLIGNMENT_RADIUS, other.position
                allignment_delta += other.velocity
                visible_boids += 1
            end
        end
        allignment_delta /= visible_boids
        @velocity_delta += allignment_delta / ALLIGNMENT_DAMPER
    end
    def calculate_hunting_delta foodstuff
        foodstuff.each do |food|
            if @position.nearby? HUNTING_RADIUS, food.position
               @velocity_delta += (food.position - @position) / HUNTUNG_DAMPER
            end
        end
    end
    def calculate_stay_visible_delta slot
        mid_x = slot.width / 2
        mid_y = slot.height / 2
        @velocity_delta -= (@position - VectorK.new(mid_x, mid_y)) / STAY_VISIBLE_DAMPER
    end
    def apply_deltas
        @velocity += @velocity_delta
        @velocity_delta = VectorK.new 0, 0
    end
    def limit_speed
        if @velocity.r &gt; MAX_SPEED
            @velocity /= @velocity.r # Create a unit vector
            @velocity *= MAX_SPEED   # Scale to max speed
        end
    end
    def move
        @position += @velocity
    end
    def draw slot, app
        app.fill rgb(0x30, 0xFF, 0xFF, 0.5)
        slot.oval :left =&gt; @position.x, :top =&gt; @position.y, :radius =&gt; RADIUS, :center =&gt; true
        slot.line @position.x, @position.y, (@position.x + @velocity.x), (@position.y + @velocity.y)
    end
end

class VectorK
    attr_reader :x, :y
    def initialize x, y
        @x = x
        @y = y
    end
    def nearby? threshold, a
        return false if a === self
        (distance a) &lt; threshold
    end
    def distance a
        Math.sqrt((@x - a.x)**2 + (@y - a.y)**2)
    end
    def / a
        if (a != 0)
            VectorK.new(@x / a, @y / a)
        else
            self
        end
    end
    def + a
        VectorK.new(@x + a.x, @y + a.y)
    end
    def - a
        VectorK.new(@x - a.x, @y - a.y)
    end
    def * a
        VectorK.new(@x * a, @y * a)
    end
    def r
        Math.sqrt(@x * @x + @y * @y)
    end
end

def random min, max
    choice = (max - min &gt; 0) ? (rand max - min) + min : 0;
end</code>
    <comment>You will need Shoes installed to run this sketch: http://code.whytheluckystiff.net/shoes/

I'm looking for speed optimizations and a reduction in code-size (but not at the expense of legibility).

Also, coming from a C/Javascript background, I'd appreciate suggestions that change my code to better follow the 'Ruby way'.

Tested with Shoes 0.r396, Windows XP.

Thank you kindly.</comment>
    <created-at type="datetime">2008-03-03T06:42:55+00:00</created-at>
    <id type="integer">248</id>
    <language>Ruby</language>
    <permalink>boids-a-shoes-application</permalink>
    <refactors-count type="integer">0</refactors-count>
    <title>Boids - A Shoes Application</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-03-03T06:42:55+00:00</updated-at>
    <user-id type="integer">517</user-id>
    <user>
      <id type="integer">517</id>
      <identity-url>http://stungeye.myopenid.com</identity-url>
      <name>stungeye</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website>http://stungeye.com</website>
    </user>
  </code>
  <code>
    <code>## Code [java]
public PreviewTreeModel(Collection&lt;FileDescription&gt; files)
	{
                // contains all files in the tree
		this.files = new ArrayList&lt;FileDescription&gt;(files);
		
		// add missing parents
		for (FileDescription description : files)
		{

                        FileDescription parent = new FileDescription(description.getFile().getParentFile(), false);

                        // add missing parents
			File parentFile = parent.getFile();
			
			List&lt;FileDescription&gt; parents = new Vector&lt;FileDescription&gt;();
			while (parentFile != null)
			{
				FileDescription parentDescription = new FileDescription(parentFile, false);

				// connected to an existing file
				if (this.files.contains(parentDescription))
				{
					this.files.addAll(parents); // complete connection
					break;
				}
				
				parents.add(parentDescription);
				parentFile = parentFile.getParentFile();
			}
		}
		
		// find roots
		this.roots = this.findRoots(files);
		
		// make sure all roots are folders
		for (FileDescription root : this.roots)
			if (root.isFile())
			{
				this.roots.remove(root);
				this.roots.add(new FileDescription(root.getFile().getParentFile(), false));
			}
	}
	
	private Collection&lt;FileDescription&gt; findRoots(Collection&lt;FileDescription&gt; descriptions)
	{
		logger.trace("findRoots(" + descriptions + ")");
		
		Collection&lt;FileDescription&gt; files;
		Collection&lt;FileDescription&gt; roots = descriptions;
		
		do
		{
			files = new Vector&lt;FileDescription&gt;(roots);
			roots = new Vector&lt;FileDescription&gt;();

			for (FileDescription fileA : files)
				for (FileDescription fileB : files)
					if (!fileA.equals(fileB))
					{
						FileDescription ancestor = this.findAncestor(fileA, fileB);
						if (ancestor != null &amp;&amp; !roots.contains(ancestor))
							roots.add(ancestor);
					}
		}
		
		while (!roots.isEmpty());
	
		return files;
	}
	
	private FileDescription findAncestor(FileDescription a, FileDescription b)
	{
		logger.trace("findAncestor(" + a + "," + b + ")");
		
		String[] partsA = a.getFile().getAbsolutePath().split("\\\\");
		String[] partsB = b.getFile().getAbsolutePath().split("\\\\");
		
		int minLength = Math.min(partsA.length, partsB.length);
		
		List&lt;String&gt; ancestorParts = new Vector&lt;String&gt;();
		for (int i = 0; i &lt; minLength; i++)
			if (partsA[i].equals(partsB[i]))
				ancestorParts.add(partsA[i]);
			
			else
				break;
		
		String ancestor = "";
		for (String part : ancestorParts)
			ancestor += part + "\\";
		
		if (ancestor.isEmpty())
			return null;
		else
			return new FileDescription(new File(ancestor), false);
	}</code>
    <comment>I would like to transform a set of files into a tree structure. The problem is that not all files making up the directory tree are in the set.
Here is an example:

The files:
A:\Folder1\Folder2\Folder3\File1.txt
A:\Folder1\File2.txt
B:\Folder4\Folder5\File3.txt
B:\Folder4\Folder5\File4.txt

The resulting tree should look like this:
A:\Folder1
-Folder2
--Folder3
---File1.txt
-File2.txt
B:\Folder4\Folder5
-File3.txt
-File4.txt
	
Technically, these are two trees, and I will need to be able to identify the root of each. The accompanying code shows the algorithm I use now. It works, but it is horribly slow. Any thoughts on how to improve its efficiency will be greatly appreciated.

Note: in the code, the object FileDescription can be thought of as a regular java.io.File. A FileDescription contains a file and a hashmap with properties. (For example, in the case of an audio file, 'volume = 10' or something along those lines.)
If I left anything unclear, I will gladly answer questions.</comment>
    <created-at type="datetime">2008-03-02T19:14:28+00:00</created-at>
    <id type="integer">247</id>
    <language>Java</language>
    <permalink>create-a-tree-out-of-a-collection-of-files</permalink>
    <refactors-count type="integer">1</refactors-count>
    <title>Create a tree out of a collection of files</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-04-14T02:30:57+00:00</updated-at>
    <user-id type="integer">516</user-id>
    <user>
      <id type="integer">516</id>
      <identity-url>http://loidthanead.myopenid.com</identity-url>
      <name>Loid</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">0</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>SCRIPT="gen_${1}.sql"
echo "--gen_${1}.sql" &gt; $SCRIPT
typeset -i counter
counter=0

echo "Get all lines that start with D|4| (this may take a while)"
LINES=$(grep "D|4|" $1)

echo "Generate insert statements to $SCRIPT"
for LINE in $LINES; do
  REFNO=$(echo $LINE | awk '{split($0,a,"|");print a[8]}')
  echo "insert into CHEQUE (datecomptable, noreference) values (to_date('2007/11/30','YYYY/MM/DD'),'${REFNO}');" &gt;&gt; $SCRIPT
        counter=counter+1
done

echo "commit;" &gt;&gt; $SCRIPT
echo "${counter} records created in $SCRIPT"</code>
    <comment>Here's a quick and dirty bash script that takes a file and looks for a particular line, then loops through those lines and splits the contents to get the 8th column and create an sql insert statement with it.  I'm not looking to make it more generic, but the for loop is unbearably slow.  How to improve it?</comment>
    <created-at type="datetime">2008-02-07T19:57:46+00:00</created-at>
    <id type="integer">229</id>
    <language>Bash</language>
    <permalink>slow-bash-for-loop</permalink>
    <refactors-count type="integer">6</refactors-count>
    <title>Slow bash for loop.</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2008-02-19T14:44:06+00:00</updated-at>
    <user-id type="integer">13</user-id>
    <user>
      <id type="integer">13</id>
      <identity-url>http://furtive.myopenid.com/</identity-url>
      <name>furtive</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
  <code>
    <code>SIXTYTWO = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a

def to_s_62(i)
  return '0' if i == 0
  s = ''
  while i &gt; 0
    i,r = i.divmod(62)
    s = (SIXTYTWO[r]) + s
  end
  s
end
</code>
    <comment>Base WHAT??? Well, 62 is the number of alphanumeric characters. Base 64 makes more sense, but nobody can agree about what the two missing characters should be. 

Ruby's Numeric#to_s(base = 10) method works great, but won't accept a base above 36. Hence my implementation, which works, but is VERY VERY SLOW.

Update: Rubinius actually does support base 62, and provides the c-code that does it: 

http://www.google.com/codesearch?hl=en&amp;q=+package:%22http://code.fallingsnow.net/svn/rubinius/trunk%22+mp_toradix_nd+show:8_qyRkJt6rs:_xHLAYhCNJI:gMCPZeygnhk&amp;sa=N&amp;cd=2&amp;ct=rc&amp;cs_p=http://code.fallingsnow.net/svn/rubinius/trunk&amp;cs_f=shotgun/external_libs/libtommath/bn_mp_toradix_nd.c#first

Seems like my algorithm is plenty fast, it's just Ruby being s.l.o.w.</comment>
    <created-at type="datetime">2007-11-01T01:42:07+00:00</created-at>
    <id type="integer">125</id>
    <language>Ruby</language>
    <permalink>base-62-encoding</permalink>
    <refactors-count type="integer">1</refactors-count>
    <title>Base 62 Encoding</title>
    <trackback-url></trackback-url>
    <updated-at type="datetime">2007-11-02T02:48:15+00:00</updated-at>
    <user-id type="integer">101</user-id>
    <user>
      <id type="integer">101</id>
      <identity-url>http://mdemare.myopenid.com/</identity-url>
      <name>michiel</name>
      <rating type="float">3.8333</rating>
      <refactors-count type="integer">13</refactors-count>
      <website nil="true"></website>
    </user>
  </code>
</codes>
