Welcome Guest, Not a member yet? Register   Sign In
Get execution time programmatically
#1

[eluser]shakur[/eluser]
Hello guys,

This is my first post so I apologize if this sounds basic or nonsense.
What I basically need is to estimate the execution time without actually displaying the view on the browser. As a matter of fact I wish to estimate execution time of a script that I keep for example in my db, but I'm not sure if this is feasible.

What I have done so far:
1. Retrieved the script from db
2. Created a new CI view from the script
3. Used the $this->load->file('myfile', TRUE); to load the file through the Loader (I guess I could use the $this->load->view here)..
4. Asking for help..

I've used $output = $this->output->get_output(); for getting the loaded file but this wont give me the actual value of {elapsed_time} but the html code.

How can I get the actual value?
Even better is it possible to achieve this without creating files or views and loading them (i.e., is it possible to estimate the execution time of an html string/scrip ?)

Thanks for your time
#2

[eluser]Dennis Rasmussen[/eluser]
How about something like this? http://ellislab.com/codeigniter/user-gui...html#using
#3

[eluser]shakur[/eluser]
Thanks Dennis,

Actually this is what I am using but this will output the result on the browser if I use the load->view and will output
Quote:<?php echo $this->benchmark->memory_usage();?>
if I use the load->file.

I need to get the execution time in my code and not the browser.

Any ideas?
#4

[eluser]danmontgomery[/eluser]
Code:
$start = microtime(TRUE);
// some code
$end = microtime(TRUE);

$elapsed = $end - $start;

echo 'Execution took '.$elapsed.' seconds.';
#5

[eluser]bl00dshooter[/eluser]
[quote author="shakur" date="1285618452"]Thanks Dennis,

Actually this is what I am using but this will output the result on the browser if I use the load->view and will output
Quote:<?php echo $this->benchmark->memory_usage();?>
if I use the load->file.

I need to get the execution time in my code and not the browser.

Any ideas?[/quote]

Remove the echo? lol.
#6

[eluser]shakur[/eluser]
[quote author="noctrum" date="1285619869"]
Code:
$start = microtime(TRUE);
// some code
$end = microtime(TRUE);

$elapsed = $end - $start;

echo 'Execution took '.$elapsed.' seconds.';
[/quote]

Thanks noctrum!
I replaced the
//Some code
with my
Code:
$this->load->file('myfile', TRUE);
and seems to work now!

thanks again
#7

[eluser]Dennis Rasmussen[/eluser]
As bl00dshooter said: remove the echo then. Store it in a variable.

OR... use the profiler (same documentation page)
#8

[eluser]gwelter[/eluser]
In case anyone is looking for this in the future, I found a way to retrieve CodeIgniter's execution time up to a given point programmatically.

I saw that the Profiler class generates the execution time like so:

Code:
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');        
$output = str_replace('{elapsed_time}', $elapsed, $output);

I wanted to store execution times in a DB table so I could see trends and problem pages, so I wrote the following in a post_controller hook:

Code:
$CI->benchmark->mark('controller_end');
$execution_time = $CI->benchmark->elapsed_time('total_execution_time_start','controller_end');

It's a bit of a hack, as it depends on the Profiler continuing to use the 'total_execution_time_start' mark. But there it is in case that helps anyone.




Theme © iAndrew 2016 - Forum software by © MyBB