我想针对同一个问题测试多个实现。例如,我有一个问题/代码部分“X”,实现“X1”和“X2”,问题“Y”,实现“Y1”、“Y2”和“Y3”。将它们想象成一些小的 10 行函数,其中的代码略有不同。我可以通过某种方式使用 git 在这些实现之间切换,从而替换相应的代码行吗?我考虑过分支,但我不知道如何以这种方式应用它。我想在每个实现之间切换到一个问题,问题的任意组合。 (所以“X1”与“Y2”,然后“X2”与“Y1”等) 然后我想衡量性能并选择要使用的最终实现。 我可以使用 git 来实现它吗?
最佳答案
这是对 git 的非正统使用,可能是一个简单的配置文件,您可以在其中选择要使用的实现是更好的方法。
但是,git 实际上非常适合“代码交换”,因此您可以像这样测试不同的实现:
$ git init .
$ git checkout -b impl1
$ cat << EOF > main.py
> print "implementation1"
> EOF
$ git add -A && git commit -m "Implementation 1"
$ git checkout -b master ## You can probably skip this ...
$ git checkout -b impl2
$ rm main.py && cat << EOF > main.py
> print "implementation2"
> EOF
$ git add -A && git commit -m "Implementation 2"
现在您可以像这样在实现之间切换:
$ git checkout impl1
$ git checkout impl2
然后测试他们的表现,或者其他什么,相互比较:
$ git checkout impl1 && time python main.py
Switched to branch 'impl1'
implementation1
python main.py 0,02s user 0,01s system 77% cpu 0,040 total
$ git checkout impl2 && time python main.py
Switched to branch 'impl2'
implementation2
python main.py 0,02s user 0,01s system 91% cpu 0,034 total
一切看起来都很正常,print
打印不同字符串的时间差不多:)
关于git - git适合做A/B测试,代码交换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23609293/