Ferdinand, somehow opening the chart of accounts is faster than getting the root account values:
* Chart of accounts test:
Without optimization:
Elapsed to compute ['debit', 'credit', 'balance'] for accounts with code 1,2,3,4,5,6,7 => 1.08773589134
With optimization:
Elapsed to compute ['debit', 'credit', 'balance'] for account 1,2,3,4,5,6,7 => 0.540276050568
101% faster (original/optimized = 2.01)
* Accounts list (first 80 accounts) test:
Without optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 0,1,10,100,1000000,...,4300498,4300502 => 2.84862804413
With optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 0,1,10,100,1000000,...,4300498,4300502 => 0.894719839096
218% faster (original/optimized = 3.18)
* Search test (searching for accounts like '%700%'):
Without optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 700,7000,7000000,7000001,7001000,7002000 => 0.0230215191841
With optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 700,7000,7000000,7000001,7001000,7002000 => 0.0187527537343
22% faster (original/optimized = 1.22)
So I suppose the problem is that the performance hit grows with the number of account levels, it takes more time to get the values for the root account, that for all its childs.
(And the Spanish chart of accounts has about five levels [43000001 is child of 4300 which is child of 430 that is child of 43 child of 4 child of 0]).
Ferdinand, somehow opening the chart of accounts is faster than getting the root account values:
* Chart of accounts test:
Without optimization:
Elapsed to compute ['debit', 'credit', 'balance'] for accounts with code 1,2,3,4,5,6,7 => 1.08773589134
With optimization:
Elapsed to compute ['debit', 'credit', 'balance'] for account 1,2,3,4,5,6,7 => 0.540276050568
101% faster (original/optimized = 2.01)
* Accounts list (first 80 accounts) test: 100,1000000, ...,4300498, 4300502 => 2.84862804413 100,1000000, ...,4300498, 4300502 => 0.894719839096
Without optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 0,1,10,
With optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 0,1,10,
218% faster (original/optimized = 3.18)
* Search test (searching for accounts like '%700%'): 7000000, 7000001, 7001000, 7002000 => 0.0230215191841 7000000, 7000001, 7001000, 7002000 => 0.0187527537343
Without optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 700,7000,
With optimization:
Elapsed to compute ['credit', 'debit', 'balance'] for account 700,7000,
22% faster (original/optimized = 1.22)
So I suppose the problem is that the performance hit grows with the number of account levels, it takes more time to get the values for the root account, that for all its childs.
(And the Spanish chart of accounts has about five levels [43000001 is child of 4300 which is child of 430 that is child of 43 child of 4 child of 0]).