当前位置:网站首页>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 .
边栏推荐
- Flutter tips: various fancy nesting of listview and pageview
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- Logstack configuration details -- elasticstack (elk) work notes 020
- 《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
- Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
- 上周热点回顾(6.27-7.3)
- Reading notes on how to connect the network - hubs, routers and routers (III)
- You can see the employment prospects of PMP project management
- Launpad | 基础知识
- Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
猜你喜欢
Horizon sunrise X3 PI (I) first boot details
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
C语言-入门-基础-语法-[主函数,头文件](二)
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
C语言-入门-基础-语法-[运算符,类型转换](六)
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
[untitled] forwarding least square method
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Implementation principle of redis string and sorted set
随机推荐
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
2022-2028 global tensile strain sensor industry research and trend analysis report
Sword finger offer 30 contains the stack of Min function
"How to connect the network" reading notes - Web server request and response (4)
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
2022-2028 global seeder industry research and trend analysis report
如何编写单元测试用例
You can see the employment prospects of PMP project management
After unplugging the network cable, does the original TCP connection still exist?
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global intelligent interactive tablet industry research and trend analysis report
Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
C語言-入門-基礎-語法-[運算符,類型轉換](六)
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global probiotics industry research and trend analysis report
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
C语言-入门-基础-语法-[变量,常亮,作用域](五)
China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
Implementation principle of redis string and sorted set