# Response time problem

**URL:** <https://www.ruby-forum.com/t/response-time-problem/132547>\
**Category:** Rails\
**Created:** [March 21, 2008, 9:33am UTC](https://www.ruby-forum.com/t/response-time-problem/132547 "2008-03-21T09:33:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [March 21, 2008, 9:33am UTC](https://www.ruby-forum.com/t/response-time-problem/132547/1 "2008-03-21T09:33:13Z")

</div>

About your application’s environment  
Ruby version 1.8.6 (x86\_64-linux)  
RubyGems version 1.0.1  
Rails version 2.0.2  
Active Record version 2.0.2  
Action Pack version 2.0.2  
Active Resource version 2.0.2  
Action Mailer version 2.0.2  
Active Support version 2.0.2  
Application root …  
Environment development  
Database adapter mysql  
Database schema version 23

The controller passes @selectedLandmarkList and @availLandmarkList to  
the view which populates a couple of selection lists with the same.  
The problem occurs when I have about 8900 items in each list.

Rendering template within layouts/admin  
Rendering data\_entry/add\_waypoint  
Rendered data\_entry/\_waypoint\_landmark\_table (0.35038)  
Rendered data\_entry/\_waypoint\_form (0.42428)  
Rendered layouts/\_admin\_menu (0.00458)  
Completed in 198.55079 (0 reqs/sec) | Rendering: 0.43816 (0%) | DB:  
0.07019 (0%) | 200 OK [[http://localhost/data\_entry/add\_waypoint](http://localhost/data_entry/add_waypoint)]

I can eliminate the error by not populating one of the lists in which  
case the response is as below.

Rendering template within layouts/admin  
Rendering data\_entry/add\_waypoint  
Rendered data\_entry/\_waypoint\_landmark\_table (0.12888)  
Rendered data\_entry/\_waypoint\_form (0.13201)  
Rendered layouts/\_admin\_menu (0.00488)  
Completed in 0.70847 (1 reqs/sec) | Rendering: 0.22962 (32%) | DB:  
0.06943 (9%) | 200 OK [[http://localhost/data\_entry/add\_waypoint](http://localhost/data_entry/add_waypoint)]

My question is how can things break by just addition of another 8900  
items to another list item? Is this a defect? I haven’t noticed  
this problem when I was on 1.2.6.

The code snippet in the controller that is different for both cases is  
something like this

if params[:selectedLandmark].length \> 50  
flash[:notice]=“…”  
—\> params[:selectedLandmark].each { |itr|  
@selectedLandmarkList += @availLandmarkList.select{ |v|  
(v[1].to\_s==itr)? true : false }  
}  
else …

If I get rid of the iterator which basically doesn’t do much, the  
response time is fast as the second list is empty. I don’t understand  
why it is slow in the first case.

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [March 22, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/response-time-problem/132547/2 "2008-03-22T23:03:52Z")

</div>

> —\> params[:selectedLandmark].each { |itr|  
> @selectedLandmarkList += @availLandmarkList.select{ |v|  
> (v[1].to\_s==itr)? true : false }  
> }  
> else …
> 
> If I get rid of the iterator which basically doesn’t do much, the  
> response time is fast as the second list is empty. I don’t understand  
> why it is slow in the first case.

Well you may not think you’re doin very much but for each thing  
selectedLandmar you’re iterating over availLandmarkList once, so if  
these are both 9000 items long then you’re ending up doing something  
81 million times. But whatever the reason is, the real answer is to  
stop guessing and use a profiler (eg ruby-prof) to find out where the  
time is going.

Fred

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [March 27, 2008, 9:47am UTC](https://www.ruby-forum.com/t/response-time-problem/132547/3 "2008-03-27T09:47:56Z")

</div>

Thanks for mentioning ruby-prof. I haven’t used a profiler for Ruby  
before and I like it. I put the RubyProf.start at the start of the  
controller action and RubyProf.stop at the end of the controller  
action and the results are as expected.

Thread ID: 46912571459280  
Total: 914.720000

%self total self wait child calls name  
46.31 759.76 423.65 0.05 336.06 8852 Array#select  
(ruby\_runtime:0}  
16.02 146.53 146.53 0.00 0.00 78251690 Fixnum#to\_s  
(ruby\_runtime:0}  
10.41 95.22 95.22 0.00 0.00 78243001 Array#[]  
(ruby\_runtime:0}  
10.37 94.89 94.89 0.00 0.00 78252170 String#==  
(ruby\_runtime:0}  
1.94 30.28 17.73 0.14 12.41 53050  
Module::Benchmark#realtime (/usr/lib/ruby/1.8/benchmark.rb:306}  
0.92 8.43 8.43 0.00 0.00 1697290 Kernel#sprintf  
(ruby\_runtime:0}  
0.76 20.79 6.94 0.01 13.84 17680 String#each\_byte  
(ruby\_runtime:0}  
0.45 4.23 4.14 0.00 0.09 53050 Mysql#query  
(ruby\_runtime:0}

I have fixed the workflow of the application so that I dont end up  
with two lists of 9K each, but if I do end up with two lists of these  
sizes that I need to manage in the future, any other tricks I can use  
or do I need to change data structures and design only?

Thanks,  
Mukund

On Mar 23, 3:02 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [March 27, 2008, 10:26am UTC](https://www.ruby-forum.com/t/response-time-problem/132547/4 "2008-03-27T10:26:58Z")

</div>

On 27 Mar 2008, at 08:46, Mukund wrote:

> Thanks for mentioning ruby-prof. I haven’t used a profiler for Ruby  
> before and I like it. I put the RubyProf.start at the start of the  
> controller action and RubyProf.stop at the end of the controller  
> action and the results are as expected.

Well the basic problems is that if your lists are of length m and n  
then what you’ve got there has complexity O(mn) (which shows up as the  
80 million calls to those functions).  
I’d also query the efficiency of any action that results in Mysql  
being queried 50000 times. If you stored @availLandmarkList as a hash  
rather than as an array you’d avoid the linear time search for every  
element of params[:selectedLandmark].

Fred.
