当前位置:网站首页>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 .
边栏推荐
- 2022-2028 global edible probiotic raw material industry research and trend analysis report
- Sequence model
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
- Lauchpad X | 模式
- GoLand environment variable configuration
- How to ensure the uniqueness of ID in distributed environment
- Clion console output Chinese garbled code
- Basic discipline formula and unit conversion
- 2022-2028 global intelligent interactive tablet industry research and trend analysis report
- 2022-2028 global optical transparency industry research and trend analysis report
猜你喜欢
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
GoLand environment variable configuration
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
CLion-控制台输出中文乱码
How does idea withdraw code from remote push
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
2022-2028 global edible probiotic raw material industry research and trend analysis report
HMS core helps baby bus show high-quality children's digital content to global developers
C语言-入门-基础-语法-[变量,常亮,作用域](五)
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
随机推荐
How to pass custom object via intent in kotlin
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Awk from entry to penetration (6) regular matching
Clion console output Chinese garbled code
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
C language - Introduction - Foundation - syntax - data type (4)
Reading notes of how the network is connected - understanding the basic concepts of the network (I)
Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
1211 or chicken and rabbit in the same cage
Markdown syntax
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
什么是uid?什么是Auth?什么是验证器?
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
awk从入门到入土(6)正则匹配
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
"How to connect the Internet" reading notes - FTTH
What is permission? What is a role? What are users?
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
Launpad | 基础知识
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了