Premature optimization is the source of all evil

Source: Internet
Author: User

In the past two days, I have done two things:

1. Restructured part of the code of a system module:

It took a day to create a 6 k multi-line Java file and add several class files to 4 K lines. My capabilities are limited, and the subsequent reconstruction cannot be completed, it is a kind of three domain attribute names with almost the same name and more than 100 fields, but the same copy method is used three times for processing, it is a kind of despair in which the mouse wheel cannot be rolled at the end of a method. (The equals, get, and set attributes of more than 100 fields come from equals, there are five or six methods of this type. Can you tell me more )......

 

2. A log processing tool is provided:

The customer asked to sort out the logs and send them to their business personnel for analysis. There were several log files, more than 20 mb. I was going to Ctrl + F, CTRL + C, one by one, CTRL + V, but later I thought about whether I could handle this matter more efficiently. After all, I have been working for two years and often do some repetitive things, which won't improve, so I decided to write a log processing tool by myself.

Find the content in a log file by keyword, parse the corresponding field, and format it into excel,

I want to simplify the log. One of the snippets looks like this:

Field1: = value1

Field2: = value2

Field3: = value3

......

......

......

Xxxxxxxxxxx: field3: value3 field4: value4 field5: value5

 

We need to obtain the names and values of field1, field3, field4, and field5.

Field3-field5 is very easy to do, direct positioning "field3:" in the row, and then take out this line to parse it

But the value of field1 is not very well processed, I was thinking of the most silly way: scan the file to parse the field3-field5, and then use the value of field3 and then scan the file to find the value of field1, slow speed

Later, I came up with a slightly smarter approach,

Set

Field1: = value1

Field2: = value2

Field3: = value3

Encapsulated into a data structure, each row is loaded with a string. During the first scan, the data structure is saved as a list, which is assumed as templist, store the field3-field5 as a data structure into a list, so that I can process the previous field3-field5 list and the templist, memory operations, the speed is improved significantly.

 

Lessons learned:

1. one of the major reasons for doing these two things is that, while the version is under development, the version plan I wrote reserves some time to cope with the demand changes, however, I spent a lot of time doing the first thing, which was thankless. Finally, I didn't need to do anything. After restructuring for a long time, the Code was so desperate that I realized it now, reading the excellent code written by others is an improvement for yourself. Reading the disgusting code written by others is also an improvement for yourself: Tell yourself not to write it like this.

2. efficiency is very important, especially in the project process. If we try to help machines solve the problem, we should not do it manually. For programmers, write as many gadgets as possible (of course, my technology is not good and the time for writing a tool may affect the progress-_-#) to help solve some manual problems, it can greatly improve your work efficiency and exercise your programming skills.

3. in the process of project development, the core skill is to evaluate as accurately as possible. If one thing is not evaluated, you can rush to do it. This is a terrible thing, if I have to re-select, I will not take the working time to re-engineer this part of code. I will first complete the task, complete it, and after work, in intern version (for Practice) reconstruction of the previous exercise.

4. Why should I use this title. -_-#

Premature optimization is the source of all evil

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.