You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Avoid useless multiple instantiations of formater in a loop
Language and platform
Swift - IOS
Rule description
What does your rule do? Why is this a green coding issue?
My rule checks if there is any formater instantiation inside a loop in the code.
We should rather initiate it upstream the loop to avoid multiple instantiation that are useless.
Triggered code:
vardate=Date()
for _ in 1...1_000_000{letformater=DateFormatter()
formater.dateFormat ="dd/MM/yyyy"letresult= formater.string(from: date)}
Instead, the following code can be proposed:
Suggested code V1 (Instantiate the formater upstream) :
vardate=Date()letformater=DateFormatter()
for _ in 1...1_000_000{
formater.dateFormat ="dd/MM/yyyy"letresult= formater.string(from: date)}
Suggested code V2 (New formatted API):
vardate=Date()
for _ in 1...1_000_000{letresult= date.formatted(date:.numeric, time:.omitted)}
Rule short description
Avoid useless multiple instantiations of formater in a loop.
Rule justification
Analyse of the duration of execution by CPU usage.
By taking the triggered and the two untrigerred codes and analyzing the execution time with the use of the CPU we obtain the following results. These measures were done with Instruments.
Triggered code
Suggested code V1 (Instantiate the formatter upstream)
Suggested code V2 (Using new formatted API)
We can observe that the triggered code takes much more time (24s) to be executed than the two other suggested code that takes a similar time to run (1.5s and 1.7s).
Severity / Remediation Cost
Severity: Major (depends of on the quantity of iteration of the loop)
Remediation: Medium (formater should not be declared inside a loop.)
Implementation principle
In any loop, ensure that no formater is instantiated.
The text was updated successfully, but these errors were encountered:
Rule title
Avoid useless multiple instantiations of formater in a loop
Language and platform
Swift - IOS
Rule description
What does your rule do? Why is this a green coding issue?
My rule checks if there is any formater instantiation inside a loop in the code.
We should rather initiate it upstream the loop to avoid multiple instantiation that are useless.
Instead, the following code can be proposed:
Rule short description
Avoid useless multiple instantiations of formater in a loop.
Rule justification
Analyse of the duration of execution by CPU usage.
By taking the triggered and the two untrigerred codes and analyzing the execution time with the use of the CPU we obtain the following results. These measures were done with Instruments.
We can observe that the triggered code takes much more time (24s) to be executed than the two other suggested code that takes a similar time to run (1.5s and 1.7s).
Severity / Remediation Cost
Severity: Major (depends of on the quantity of iteration of the loop)
Remediation: Medium (formater should not be declared inside a loop.)
Implementation principle
In any loop, ensure that no formater is instantiated.
The text was updated successfully, but these errors were encountered: