以下两段代码哪一段更快或相同,为什么?
Which of the following two pieces of code is faster or the same, and why?
-记录(测试,{a = 10})。
test(Test) when is_record(Test,test) -> somethings.
或
test(#test{} = Test) -> somethings.
哪个更快或相同?为什么。
也许第二个one.There在运行时没有记录的概念,只有元组。
21> rd(test, {a = 10}).
test
22> erlang:is_record({test, 1}, test).
true
用编译器测试并不难。
为此,我编写了这个模块......
-module my_mod.
-export [t1/1, t2/1].
-record(test, {a = 10}).
t1(Test) when is_record(Test,test) -> somethings.
t2(#test{} = _Test) -> somethings.
然后,我 运行 erlc -E my_mod.erl
这是生成的扩展代码:
-file("my_mod.erl", 1).
-module(my_mod).
-export([t1/1,t2/1]).
-record(test,{a = 10}).
t1({test, _} = Test) when true ->
somethings.
t2({test, _} = _Test) ->
somethings.
所以,基本上……是一样的。使用 is_record(Test, test)
会增加一个无用的守卫 (true
),但这不会对 speed.
产生影响
此外,如果您使用 erlc -S my_mod.erl
生成程序集列表,您将获得:
{module, my_mod}. %% version = 0
{exports, [{module_info,0},{module_info,1},{t1,1},{t2,1}]}.
{attributes, []}.
{labels, 9}.
{function, t1, 1, 2}.
{label,1}.
{line,[{location,"my_mod.erl",6}]}.
{func_info,{atom,my_mod},{atom,t1},1}.
{label,2}.
{test,is_tagged_tuple,{f,1},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.
{function, t2, 1, 4}.
{label,3}.
{line,[{location,"my_mod.erl",8}]}.
{func_info,{atom,my_mod},{atom,t2},1}.
{label,4}.
{test,is_tagged_tuple,{f,3},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.
{function, module_info, 0, 6}.
{label,5}.
{line,[]}.
{func_info,{atom,my_mod},{atom,module_info},0}.
{label,6}.
{move,{atom,my_mod},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 8}.
{label,7}.
{line,[]}.
{func_info,{atom,my_mod},{atom,module_info},1}.
{label,8}.
{move,{x,0},{x,1}}.
{move,{atom,my_mod},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
如您所见,这两个函数实际上是相同的:
{function, …, 1, …}.
{label,…}.
{line,[{location,"my_mod.erl",…}]}.
{func_info,{atom,my_mod},{atom,…},1}.
{label,…}.
{test,is_tagged_tuple,{f,…},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.
-记录(测试,{a = 10})。
test(Test) when is_record(Test,test) -> somethings.
或
test(#test{} = Test) -> somethings.
哪个更快或相同?为什么。
也许第二个one.There在运行时没有记录的概念,只有元组。
21> rd(test, {a = 10}).
test
22> erlang:is_record({test, 1}, test).
true
用编译器测试并不难。 为此,我编写了这个模块......
-module my_mod.
-export [t1/1, t2/1].
-record(test, {a = 10}).
t1(Test) when is_record(Test,test) -> somethings.
t2(#test{} = _Test) -> somethings.
然后,我 运行 erlc -E my_mod.erl
这是生成的扩展代码:
-file("my_mod.erl", 1).
-module(my_mod).
-export([t1/1,t2/1]).
-record(test,{a = 10}).
t1({test, _} = Test) when true ->
somethings.
t2({test, _} = _Test) ->
somethings.
所以,基本上……是一样的。使用 is_record(Test, test)
会增加一个无用的守卫 (true
),但这不会对 speed.
此外,如果您使用 erlc -S my_mod.erl
生成程序集列表,您将获得:
{module, my_mod}. %% version = 0
{exports, [{module_info,0},{module_info,1},{t1,1},{t2,1}]}.
{attributes, []}.
{labels, 9}.
{function, t1, 1, 2}.
{label,1}.
{line,[{location,"my_mod.erl",6}]}.
{func_info,{atom,my_mod},{atom,t1},1}.
{label,2}.
{test,is_tagged_tuple,{f,1},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.
{function, t2, 1, 4}.
{label,3}.
{line,[{location,"my_mod.erl",8}]}.
{func_info,{atom,my_mod},{atom,t2},1}.
{label,4}.
{test,is_tagged_tuple,{f,3},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.
{function, module_info, 0, 6}.
{label,5}.
{line,[]}.
{func_info,{atom,my_mod},{atom,module_info},0}.
{label,6}.
{move,{atom,my_mod},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 8}.
{label,7}.
{line,[]}.
{func_info,{atom,my_mod},{atom,module_info},1}.
{label,8}.
{move,{x,0},{x,1}}.
{move,{atom,my_mod},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
如您所见,这两个函数实际上是相同的:
{function, …, 1, …}.
{label,…}.
{line,[{location,"my_mod.erl",…}]}.
{func_info,{atom,my_mod},{atom,…},1}.
{label,…}.
{test,is_tagged_tuple,{f,…},[{x,0},2,{atom,test}]}.
{move,{atom,somethings},{x,0}}.
return.