当前位置:网站首页>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 .
边栏推荐
- Industry depression has the advantages of industry depression
- Awk from getting started to digging in (9) circular statement
- ArrayBuffer
- Live in a dream, only do things you don't say
- Awk from digging into the ground to getting started (10) awk built-in functions
- 地平线 旭日X3 PI (一)首次开机细节
- In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
- 2022-2028 global special starch industry research and trend analysis report
- 165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
- Educational Codeforces Round 115 (Rated for Div. 2)
猜你喜欢
](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C语言-入门-基础-语法-[变量,常亮,作用域](五)

AMLOGIC gsensor debugging

MySQL foundation 02 - installing MySQL in non docker version

2022-2028 global strain gauge pressure sensor industry research and trend analysis report
![[error record] no matching function for call to 'cacheflush' cacheflush();)](/img/79/c00f9c835606b2dce1d342ec368d24.jpg)
[error record] no matching function for call to 'cacheflush' cacheflush();)

How do microservices aggregate API documents? This wave of show~

Explain TCP protocol in detail three handshakes and four waves

Sword finger offer 30 contains the stack of Min function
](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C语言-入门-基础-语法-[运算符,类型转换](六)

How to ensure the uniqueness of ID in distributed environment
随机推荐
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
A subclass must use the super keyword to call the methods of its parent class
Reading notes of how the network is connected - understanding the basic concepts of the network (I)
Awk from entry to earth (8) array
Launchpad x | mode
Opencv environment construction (I)
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
The old-fashioned synchronized lock optimization will make it clear to you at once!
Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
Codeforces Round #793 (Div. 2)(A-D)
Trees and graphs (traversal)
Clion console output Chinese garbled code
Industry depression has the advantages of industry depression
Awk from digging into the ground to getting started (10) awk built-in functions
Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
《网络是怎么样连接的》读书笔记 - FTTH
Tkinter Huarong Road 4x4 tutorial II
2022-2028 global elastic strain sensor industry research and trend analysis report
Mantis creates users without password options
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)