草庐IT

mark-compacts

全部标签

ruby - 为什么 map {}.compact 比 each_with_object([]) 更快?

我做了一些基准测试:require'benchmark'words=File.open('/usr/share/dict/words','r')do|file|file.each_line.take(1_000_000).map(&:chomp)endBenchmark.bmbm(20)do|x|GC.startx.report(:map)dowords.mapdo|word|word.sizeifword.size>5end.compactendGC.startx.report(:each_with_object)dowords.each_with_object([])do|word

pytest参数化:@pytest.mark.parametrize

内置的pytest.mark.parametrize装饰器可以用来对测试函数进行参数化处理。下面是一个典型的范例,检查特定的输入所期望的输出是否匹配:test_expectation.pyimportpytest@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42),])deftest_eval(test_input,expected):asserteval(test_input)==expected装饰器@parametrize定义了三组不同的(test_input,expected)数据,

pytest参数化:@pytest.mark.parametrize

内置的pytest.mark.parametrize装饰器可以用来对测试函数进行参数化处理。下面是一个典型的范例,检查特定的输入所期望的输出是否匹配:test_expectation.pyimportpytest@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42),])deftest_eval(test_input,expected):asserteval(test_input)==expected装饰器@parametrize定义了三组不同的(test_input,expected)数据,