Series of countermeasures footprints & # 8211; Comment on optimize code

Most of the newbie often make a basic mistake : Excessive concern about the execution speed of the code, the programming language . This is a habit to give up!

In today's article, I will explain the code optimization: Optimize code in the most pro! When you need to optimize, when should you not optimize the code?

What is Optimize code?

Optimize code is code editing / rewriting to reduce capacity, limit input / output, speed up execution, reduce the amount of hardware to use.

When I was in school, I learned about Big O Notation , the optimized algorithm will run faster, or use less memory. Most universities teach C, C ++, work directly with memory, teachers always consider: Code written must be as optimal as possible.

Taking the informatics exam, the written code runs faster, less memory consumption, the higher the score. Interviews by some large companies also require that the code be better optimized. This led to many of you have thought: Yes maximum priority to optimize the code!

comic

In the software industry , this is not true ! Why so? Read below.

When do not optimize the code?

In the previous article about software industry quotations, I have a quote from Donald Knuth's quotations on optimize code.

3

Originally this sentence is as follows:

We should ignore the petty optimizations, in about 97% of all cases: Optimize code soon is the source of every crime. However, we should not ignore the remaining optimize of 3%.

In the workplace, performance is just one aspect of code. As I said many times, your code will be read, upgraded and maintained by many others. Therefore, the code should be clear, easy to read and understand, then calculate the performance. According to experience , if you are too focused on optimizing your code, your code may become difficult to read .

In most cases, the simplicity and clarity of code is more important than performance many times . Good code is really easy to read, understand and maintain.

Join the largest Web event in Vietnam at: Vietnamwebsummit.com
Join the largest Web event in Vietnam at: Vietnamwebsummit.com

In addition, in most cases, you will not know what functions to optimize, what code to optimize. If this is not determined, blindly optimizing the code will be like "fortune tellers watching elephants," which takes time and nothing.

slide_3

I have encountered a case like the following. Have you asked me, "Dapper performance is faster than Entity Framework, should the web be slow to replace with EF with Dapper?". I suggest they use the tool to measure performance first.

Time to read data from DB is only 50ms, having twice as fast can only save 25ms. It turns out that slow web is due to multiple Ajax calls, load CSS / JS and long images. Just add the cache, minify and bundle back to CSS / JS, adjust the image quality as the speed has increased 2/3 times without fixing much touching the code.

Some rules to remember when optimizing the code

As mentioned in the quotations, we should not optimize the code early , rather than not optimizing the code . Before deciding to optimize the code, try applying the following rules.

When deciding to optimize, follow these steps:

  1. Code must run
  2. The code must run properly
  3. Use the profiler to identify slow-running code
  4. Optimize code to make code run faster
  5. Confirm that the code is running and running correctly
  6. Repeat with the profiler to confirm the optimized code

Another advice from optimize experts:

  1. Do not optimize the code
  2. Don't optimize the code quickly, leave it behind (For professionals)
  3. Need to measure before optimizing.

Another important thing to remember is:   Ask yourself if you really need to optimize ? For example, the sales website has a report export function. This function runs for 1 hour. Too slow, maybe you have to optimize? Wait, consider the following cases. Suppose you take 10 hours to optimize this function, the running time is about 30 minutes.

opt

If this function is used by admin every day, after 1 month they will save 15 hours. Removing the optimize output is correct.

However, if the function for a whole month or half a year runs once, it will take several years for your efforts to remove the new optimize … return the money. Instead of optimizing it, you should take the time to optimize more useful, more used functions.

Exception

As I said, in 97% of cases you don't need to worry about optimizing the code. However, there are some exceptions (the remaining 3%) that the optimize code is quite important:

  • Code embedded systems, low memory, small processors. Now the code needs to be optimized to match the system.
  • The code of hardware, drivers, OS, … performance here is extremely important.
  • Code real-time systems, need precision and fast to every millisecond.
  • Code some libraries / frameworks are used by many people (file compression, mapper, parser, …). Since the code is used a lot, if the code runs fast, it will save a lot of program time.
  • Code implement algorithms, participate in exams.
  • The system code is very large for many users (Google, Amazon, StackOverflow , …), now just 10-20% faster code also saves countless hours of users.

server_01

What I want to say after the article is:

Performance of the code is also quite important, but it is not the most important thing

A good programmer will know how to write code that runs fast, know how to optimize the code

The excellent programmer will know when to optimize the code, when not

Some sources of reference

ITZone via toidicodedao

Share the news now