9800daba77be079c6c250a9bfe4a8e6c

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

#!/usr/bin/ruby
require 'builder'
require 'fasterCSV'
require 'postgres'

file = File.new("testing.xml", "w")
xml = Builder::XmlMarkup.new( :target => file, :indent => 4 )
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8", :standalone=>"no"
xml.declare! :DOCTYPE, :questestinterop, :SYSTEM, "ims_qtiasiv1p2.dtd"
pghost =  'localhost'
pgport = 5432
pgtbl = 'tweek'
dbname = 'postgres'

file.print(xml.target!)
# Loop for each record
(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,'')
res_aqo = db.exec(query_aqo)
  res_q = db.exec(query_q)
  res_qn = db.exec(query_qn)
  res_qo = db.exec(query_qo)
# 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
num_options = res_qo.to_a.length
  rcardinality = "single"
  rtiming = "No"
  shuffle = "Yes"
# Start the XML fun
  # Question the participant!
  xml.questestinterop do
    xml.item(:title => item_title, :ident => item_ident) do
      # Presentation
      xml.presentation do
        xml.material do
          xml.mattext(question_text)
        end
        xml.response_lid(:ident => "all filer", :rcardinality => rcardinality, :rtiming => rtiming) do
          xml.render_choice(:shuffle => shuffle) do
            for j in (0..num_options -1)
              xml.response_label(:ident => response_ident[j])do
                xml.material do
                    xml.mattext(response_mattext[j])                   
                end
              end
            end
          end
        end
      end
#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 => response_mattext[k], :continue => "Yes") do
            xml.conditionvar do
              xml.varequal( response_ident[k], :respident => module_code)
            end
          xml.setvar(response_boolean_tf[k], :action => "Set")
          xml.displayfeedback(:feedbacktype => "Response", :linkrefid => "")
          end
        end
      end
#find the number of answers and repeat      
      for l in (0..rfeedback - 1)
        xml.itemfeedback(:ident => response_mattext[l], :view => "Candidate") do
          xml.material do
              xml.mattext(response_feedback[l])
          end
        end
      end
    end
  end
end
file.close

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel