Tutorial :Better performance or better structure [closed]



Question:

Better performance:

list($result1,$result2,$result3,$result4)=get_all_result();  

The number of total sql queries are fewer,and performance is a bit better.But it's hard to reuse.

Better structure:

$result1=module1_get_result();  $result2=module2_get_result();  $result3=module3_get_result();  $result4=module4_get_result();  

It'll need more queries,so performance is a bit worse.But the structure is more clear.

Which do you perfer?


Solution:1

That depends.

For critical paths of the application you may have to write high performance code that can be hard to read and maintain, but optimizations are best done when actual problems have been identified. For the rest I would generally prefer maintainability over high performance.


Solution:2

Well, how important is the performance?

Is it worth it compared to the increased reuse/maintainability of the later?


Solution:3

Since your code looks like PHP code, I'll assume that is PHP.

It's better if you use array, both performance and readable.

$results = get_all_result();  //the above should return an array, so you can access  //$results['result1'], $results['result2'], $results['result3'], $results['result4']  

Fewer call is better for performance. Store the result in array or object for better readability.

Also, premature optimization is evil. You should create code that maintainable, because creating is just about 10% of what you do. 90% of the code life cycle is to maintain it, either bug fix, change feature, add new feature, etc. Maintainable code will also have fewer bug, since the way it written, the bug will easier to spotted.


Solution:4

If get_all_result() returns a numerically indexed array (1, 2, 3, ...) why not:

extract(get_all_result(), EXTR_PREFIX_ALL, 'result');  

extract() seems like a perfect candidate for this.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »