Get to know your compiler! Given a non-trivial task with many solution paths, a sensible Computer Scientist would utilize the path that offers an optimum balance of time/space complexity. The seasoned Software Engineer also understands getting the task done is only half the job; the final code must also be maintainable. One way to accomplish this is by making the source accessible to others. Thanks to the diligent work of the Computer Scientist, the Software Engineer can spend her time designing a readable framework without having to worry (at least not too much) about macro-optimizing the programs.
Because ideally your compiler provided a full range of optimization others won’t even need to be aware of. Just as the introduction of context-free language for machine code compilers made programming accessible to the average user, so too do today’s modern compilers—by optimizing code to be more efficient without requiring a programmer to sift through unintelligible code in the hopes of improving performance.
For instance, consider a task to be accomplished programmatically in which the output size is unimportant. The major concern is its performance to the tick of the processor. One may assume that—in order to save ticks on stack operations—the task would be best achieved within a very long processing unit (i.e. function). But this would go against the adage that a function should perform one task and one task only. Modulating code into functions makes it more maintainable even if those functions are called just once. Depending on the specifics, if the task were performed in C++ for instance, we could take advantage of GNU C++ Compiler’s optimizing options for heuristically inlining functions: -finline-functions, -finline-small-function, and -finline-functions-called-once.
The debate of whether to pre-fetch collection size when looping through it, serves as another example of when it’s wise to mind the compiler. Compare the following snippets of code:
Without knowing too much about the language or the compiler, one would assume the snippet on the left might generate more efficient code. After all, it appears the other snippet will evaluate args.Length at every iteration. But in C# this is not the case. Take a look at the optimized code generated after the Just In Time Compiler has had a pass at it (I’ve omitted the interrupt instructions that allowed me to see the code after JIT):
Even with no experience in Assembly, one notices the code generated is similar in length and form and, most importantly, so is the portion that gets looped over. One might wonder why the mov instructions on line 14 differ greatly on their operands and why not every CISC instruction executes at a constant rate. But suffice to say this does not impact the speed of execution in the end product. The way assemblers generate machine code is outside this post’s scope.
At a glance, these examples may seem trivial but they are significant in that they encourage the reader to take a closer look at what their compiler of choice does behind the scenes, thus allowing your engineer to focus more of her efforts on making the code more readable overall.
Got a question about your organization’s code-optimization strategies? Or any other question around mobile strategy for that matter? Then give us a call! We’d love to hear from you.
Software Architect at Propelics
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.