Tutorial :Better performance or better structure [closed]


Better performance:


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?


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.


Well, how important is the performance?

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


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.


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
Next Post »