# How to display the rendering time of a page?

**URL:** <https://www.ruby-forum.com/t/how-to-display-the-rendering-time-of-a-page/119926>\
**Category:** Rails\
**Created:** [November 12, 2007, 11:13am UTC](https://www.ruby-forum.com/t/how-to-display-the-rendering-time-of-a-page/119926 "2007-11-12T11:13:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![voodoorai2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9dc877/32.png) [@voodoorai2000](https://www.ruby-forum.com/u/voodoorai2000)\
**Post date:** [November 12, 2007, 11:13am UTC](https://www.ruby-forum.com/t/how-to-display-the-rendering-time-of-a-page/119926/1 "2007-11-12T11:13:24Z")

</div>

Hi,

If an admin is logged in, I’m trying to display how long the page took  
to render, somewhere at the bottom of the page.

I tried with an around\_filter :time\_an\_action in application.rb

def time\_an\_action  
started = Time.now  
yield  
@time\_to\_render = Time.now - started  
end

In the layout I added this line:  
\<%= @time\_to\_render%\>

However, as the time\_an\_action method has already yielded the page  
before the instance variable @time\_to\_render has been given a value,  
it does not display it.

Trying to execute an AJAX call inside the filter or redirecting to  
another action, for example:  
def time\_an\_action  
started = Time.now  
yield  
@time\_to\_render = Time.now - started  
redirect\_to :controller =\> “admin”, :action =\> “display\_time”  
end

def display\_time  
render :update do |page|  
page.replace\_html “time\_to\_render”, @time\_to\_render  
end  
end

Gives the error of “You can’t render or redirect twice in the same  
action”…

Any suggestions?

Thanks!

Rai

---

<div class="post-metadata">

**Author:** ![voodoorai2000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9dc877/32.png) [@voodoorai2000](https://www.ruby-forum.com/u/voodoorai2000)\
**Post date:** [November 13, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/how-to-display-the-rendering-time-of-a-page/119926/2 "2007-11-13T16:54:52Z")

</div>

Hi Rai,

This is untested code, hope it works…

Class MyTimer  
attr\_reader :elapsed\_time  
def initiailize  
@elapsed\_time = nil  
end  
def before\_filter  
@start\_time = Time.now  
end  
def after\_filter  
@end\_time = Time.now  
@elapsed\_time = @end\_time - @start\_time  
end  
end

in our layout.rhtml at the bottom, i guess u can put this code.

 Time take to render \<%= @elapsed\_time %\> 

And in application.rb (controller)  
require ‘mytimer’  
class Application \< ActionController::Base  
around\_filter MyTimer.new  
end

P.s:  
The above code is untested, you can further optimize it. Sorry, i  
don’t have ruby installed in this system.  
May find syntax error.

Hope it works,

Regards,  
Raghu Kumar K
