当前位置:网站首页>Ultimate bug finding method - two points
Ultimate bug finding method - two points
2022-07-04 09:19:00 【lucifer】
Ultimate search bug Dafa - Two points
Hello everyone , I am a lucifer. Today, I'd like to share with you some common things in my daily work debug is !
Binary code
When I find a difficult to understand and find bug When , my 「 The ultimate solution 」 It's binary code .
For example, delete the file first a Half of the code ( about ), Then test whether the problem is still .
If the problem is gone , Then we found the code that went wrong . If the problem persists , Then we continue to use the same method , Continue deleting files a Half of the code ( about ), Then test whether the problem is still . Repeat the above steps , Until you find the wrong code
This method is very useful when I have no thoughts or help others locate problems . Due to this practice, the time complexity is roughly logn, Therefore, we can roughly locate the problem code in just a few times . Similarly , We can also perform bisection in multiple files at the same time .
Submit in two
More time , We found one between two releases bug. There are several between these two releases commit Of , also commit There's a lot more ( Dozens or even hundreds ).
We can also use a similar method .
First switch to the intermediate submission between two releases x( Even if you have to submit x The distance difference between two releases is the smallest ).
*In fact, the minimum distance difference is either 0, Or 1
*
Verify whether the problem exists . If it doesn't exist , We are not sure that this is the problem of submission , You might as well mark the current submission first c by good. If there is , You may wish to mark the current submission c by bad.

Go through the mark on it , We can find the earliest presentation bad The submission of , And the most important thing is that the complexity is logn, among n For the total number of submissions we need to verify . obviously , This workload is more than checking one by one commit Much faster .
*Don't understand the principle ? We'll talk about it later .
*
Git Binary search in
git Developers of also thought of this , So it provides bisect Command to help us do the above things .
Use bisect Problem finding mainly depends on the following three commands :
git bisect start
Start a search ,start You can add good and bad The submission point of :
git bisect start [rev bad] [rev good]
If not good and bad The submission point of , that git Will wait until we use good and bad The command specifies the corresponding submission point and starts searching
git bisect good
It is correct to mark a submission point , You can add rev To specify a specific submission point , If not , Then the current submission point is marked by default .
git bisect bad
It is used to mark a submission point that contains problems , If bad You can add rev To specify a specific submission point , If not , Then the current submission point is marked by default .
The principle behind it
Let's fill the hole in front . Why go through such a mark , We can find the first problem ( Mark bad) Submission of ? And the time complexity is .
There is just a principle of force buckle , Let's use it directly .
Title address (278. First wrong version )
https://leetcode-cn.com/problems/first-bad-version/
Title Description
You are the product manager , Currently leading a team to develop new products . Unfortunately , The latest version of your product doesn't pass the quality test . Because each version is based on the previous version , So all versions after the wrong version are wrong .
Suppose you have n A version [1, 2, ..., n], You want to find out the first wrong version that caused all subsequent versions to go wrong .
You can call bool isBadVersion(version) Interface to determine version number version If there is an error in unit test . Implement a function to find the first wrong version . You should try to minimize calls to API The number of times .
Example 1:
Input :n = 5, bad = 4
Output :4
explain :
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
therefore ,4 It's the first wrong version .
Example 2:
Input :n = 1, bad = 1
Output :1
Tips :
1 <= bad <= n <= 231 - 1
Ideas
It can be seen that , This process is the same as that described above . And our goal is to find the first submission that goes wrong .
Just to be clear :
If a version x yes good Of , that [1, x] All submissions between must be good Of , Therefore, the version to be detected becomes [x+1,n] If a version x yes bad Of , that [x, n] All submissions between must be bad Of . Because we are looking for the first problematic version , Therefore, the version to be detected becomes [1,x-1]
So no matter what version we detect good still bad, We can all reduce the number of versions to be tested to half , That is to say, we can be in Find the first problematic version within times .
If you've seen my dichotomy , You should know this is what I summarized 「 The leftmost two 」.
Code
Language support :Python3
Python3 Code:
class Solution:
def firstBadVersion(self, n):
l, r = 1, n
while l <= r:
mid = (l + r) // 2
if isBadVersion(mid):
# shrinkage
r = mid - 1
else:
l = mid + 1
return l
「 Complexity analysis 」
Time complexity : Spatial complexity :
summary
Dichotomy is widely used in daily work , Dichotomy bug Is one of the most practical skills . The simplest two-point search bug You can delete the contents of the file . And if there are many files , It's not convenient , At this time, we can use binary submission to complete .
The principle is also very simple , It's a simple leftmost two . If you are not familiar with this , It is strongly recommended to look at the dichotomous topics mentioned in the article , A summary of 20000 words will definitely make you gain .
边栏推荐
- C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
- AMLOGIC gsensor debugging
- DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
- 2022-2028 global tensile strain sensor industry research and trend analysis report
- Awk from entry to earth (18) GAW K line manual
- 《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
- Relationship and operation of random events
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
- 2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
猜你喜欢

How to batch change file extensions in win10

After unplugging the network cable, does the original TCP connection still exist?

HMS core helps baby bus show high-quality children's digital content to global developers

MySQL foundation 02 - installing MySQL in non docker version

Ehrlich sieve + Euler sieve + interval sieve

Markdown syntax

Logstack configuration details -- elasticstack (elk) work notes 020

At the age of 30, I changed to Hongmeng with a high salary because I did these three things

How does idea withdraw code from remote push

Solve the problem of "Chinese garbled MySQL fields"
随机推荐
After unplugging the network cable, does the original TCP connection still exist?
HMS core helps baby bus show high-quality children's digital content to global developers
随机事件的关系与运算
Basic data types in golang
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
Awk from entry to earth (14) awk output redirection
Live in a dream, only do things you don't say
Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
Mac platform forgets the root password of MySQL
Launchpad x | mode
Nurse level JDEC addition, deletion, modification and inspection exercise
Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Review of last week's hot spots (6.27-7.3)
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
Talk about single case mode
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)