Jian Lin wrote:
> Gary Doades wrote:
>> "Starting inserting records"
>>   31.996000   0.639000  32.635000 ( 35.356000)
>> 30000
>>
>> For 30000 inserts with all indexes:
>>
>> "Starting inserting records"
>>   32.795000   0.982000  33.777000 ( 37.103000)
>> 30000
>>
>> I Don't know if it is the hash lookup code or ActiveRecord that is
>> gobbling up the time, but it certainly isn't the database.
> 
> 
> by the way... interesting to find out the CPU time is so much... i 
> thought this code is I/O bound at first...
> 
> by the way I am using Rails 2.3.2, Ruby 1.8.6 patchlevel 287... on 
> Windows 7.
> 
> hm... the Hash look up time should be really small... for example, if i 
> just write all data to a text file, it should be really fast...  it 
> might be ActiveRecord, although I though ActiveRecord merely translate 
> the method into a SQL statement and so shouldn't be so CPU intensive.
> 

Aha.... It looks like ActiveRecord has an enormous overhead in 
creating/saving records.

If you change the inserts to this....

puts Benchmark.measure {
Phrase.transaction do
   all_phrases.each do |phrase|
     Phrase.connection.execute("insert into phrases(s,frequency,length) 
values('#{phrase}',#{frequencies[phrase]},#{lengths[phrase]})")
   end
end
}

you get this:

"Starting inserting records"
   1.123000   0.686000   1.809000 (  5.096000)
30000

Which is exactly what you want I think :)

This falls down of course if the phrases contain single quote characters 
or are from an untrusted source. You will need to at least escape quotes 
before putting them in the insert statement. This will slow it down a 
bit, but not as much as before I would think.

Note to self: Don't attempt to do lots of records creations with 
standard AR code!

Cheers,
Gary.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to