Better performance or better structure.

Best performance:

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

The number of sql queries is less, and the performance is slightly better. But it is difficult to reuse.

The best structure:

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

This will require more queries, so performance is slightly worse. But the structure is more understandable.

What are you doing?

+4
source share
4 answers

It depends.

For critical application paths, you may need to write high-performance code that is difficult to read and maintain, but optimization is best done when actual problems are identified. Otherwise, I would prefer maintainability over high performance.

+2
source

Well, how important is performance?

Is this worth comparing with the increased reuse / maintainability of a later one?

+1
source

Since your code looks like PHP code, I will assume that this is PHP.

Better if you use an array, both performance and readability.

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

A smaller call is better for performance. Store the result in an array or object for better readability.

In addition, premature optimization is evil. You must create code that is supported, because creation is about 10% of what you do. 90% of the life cycle of a code is its support, fixing bugs, changing a function, adding a new function, etc. Supported code will also have fewer errors, as it is written, the error will be easier to recognize.

0
source

If get_all_result() returns a numerical index array (1, 2, 3, ...), why not:

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

extract() seems like the perfect candidate for this.

-one
source

Source: https://habr.com/ru/post/1299870/


All Articles