Question

In: Computer Science

Runtime efficiency and code simplicity are often competing goals. How can you deal with this problem?...

Runtime efficiency and code simplicity are often competing goals. How can you deal with this problem? Is it possible to have code that is both simple and efficient?

Solutions

Expert Solution

Runtime efficiency and code simplicity are often competing goals. How can you deal with this problem?

Run-time efficiency completely depends on the  algorithm which a programmer is going to be  implemented in his program. For example, there are two algorithms which can be implemented to for a piece of program with n inputs. And below is the table which shows the time taken for both algorithms depending on its input size.

n (list size) Algorithm A run-time
(in nanoseconds)
Algorithm B run-time
(in nanoseconds)
16 8 100,000
63 32 150,000
250 125 200,000
1,000 500 250,000

So, from the above table, it is clear that algorithm A  is far superior in efficiency to that of algorithm B. So, to implement an algorithm we need to understand the basic intention of the program. Thus we can make sure to implement an algorithm which will be simple and optimized. A proper framework can be built to design the algorithm which will be highly efficient and simple. Big O notation is a convenient way to express the run time of an algorithm. Below approaches a programmer needs to follow while writing the code to make it both efficient and simple as well.

  • Proper naming convention  of variable name.
  • Write comments and documentation.
  • Avoiding hard-coding.
  • Need to use proper memory layout by using the right data structures.
  • Need to follow good architecture (for example; micro-service architecture ) while designing the algorithm.
  • Consistent indentation.
  • Avoid deep nesting of loops
  • Break up functions or methods or modules into smaller ones.

Is it possible to have code that is both simple and efficient?

Yes, it is possible to have code that is both simple and efficient. Code or algorithms that has a lot of statements, lot of nested loops, hard coded values of variables, poor naming convention, no comments, is often called a complex code. This type of code is not efficient as well. But by following above discussed approaches, we can write a simple and efficient code as well.


Related Solutions

Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of...
Think of the different goals of policymaking: efficiency, growth, stability, and equity. Why do they often...
Think of the different goals of policymaking: efficiency, growth, stability, and equity. Why do they often conflict? Why can an economy not achieve multiple goals simultaneously?
How would you tackle the welfare problem? State the goals you would seek, and explain how...
How would you tackle the welfare problem? State the goals you would seek, and explain how the measures you propose would work to meet those goals.
How can international trade enhance economic efficiency? How can immigration enhance economic efficiency?
How can international trade enhance economic efficiency? How can immigration enhance economic efficiency?
1. In this class you have been introduced to the goals of efficiency (achieving the most...
1. In this class you have been introduced to the goals of efficiency (achieving the most net benefit) and equity (achieving more equal outcomes). The tradeoff between these goals can be seen in markets such as health care, labor, and housing. Explain how a policy maker’s values regarding the importance of efficiency versus equity would cause them to intervene in a market, or leave it alone. 2. Some people might argue that Monopoly is better than Perfect Competition, because monopolies...
2. In this class you have been introduced to the goals of efficiency (achieving the most...
2. In this class you have been introduced to the goals of efficiency (achieving the most net benefit) and equity (achieving more equal outcomes). The tradeoff between these goals can be seen in markets such as health care, labor, higher education, and housing. Explain how a policy maker’s values regarding the importance of efficiency versus equity would cause them to intervene in a market, or leave it alone. (Chapter 1, efficiency-equity tradeoff) (SBS2, SBS4)
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem...
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandstop filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce the below is the Problem 1 initail code .. you can use it Matlab The following cilab code generates a 10-second “chirp”...
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G,...
For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of ACGTACGTTT, there are...
What are the goals of the Biology Gen III microplate and how can you determine an...
What are the goals of the Biology Gen III microplate and how can you determine an outcome from the basis that can be observed. How are bacteria added and how is growth measured in the Biology Gen III? Likewise, how can the selected nutrients and the growth patterns in the Gen III microplate be used to identify common clinical and industrial strains of bacteria quickly?
How do the operational goals of STARBUCKS align with the financial goals? Can anyone summarize it...
How do the operational goals of STARBUCKS align with the financial goals? Can anyone summarize it after viewing the 10K Annual report of STARBUCKS?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT