我認爲這會花費你一個間接。我這麼說是因爲我把這個代碼
-module(testit).
-export([get_toolkit/0, long/0, short/0]).
get_toolkit() ->
_ = lists:seq(1,100),
{ok, thing}.
tk() ->
get_toolkit().
long() ->
get_toolkit(),
{ok, thing2}.
short() ->
tk(),
{ok, thing3}.
,併產生通過erlc -S testit.erl的ASM這給了我
SNIP
{function, tk, 0, 4}.
{label,3}.
{line,[{location,"testit.erl",8}]}.
{func_info,{atom,testit},{atom,tk},0}.
{label,4}.
{call_only,0,{f,2}}.
{function, long, 0, 6}.
{label,5}.
{line,[{location,"testit.erl",11}]}.
{func_info,{atom,testit},{atom,long},0}.
{label,6}.
{allocate,0,0}.
{line,[{location,"testit.erl",12}]}.
{call,0,{f,2}}.
{move,{literal,{ok,thing2}},{x,0}}.
{deallocate,0}.
return.
{function, short, 0, 8}.
{label,7}.
{line,[{location,"testit.erl",15}]}.
{func_info,{atom,testit},{atom,short},0}.
{label,8}.
{allocate,0,0}.
{line,[{location,"testit.erl",16}]}.
{call,0,{f,4}}.
{move,{literal,{ok,thing3}},{x,0}}.
{deallocate,0}.
return.
- 中列出的第一個功能剪斷,它是「短手「功能,tk/0。
- 第二個是長函數調用get_toolkit/0,
- 第三是它使用TK短功能/ 0速記
的ASM表明最後功能(一個使用tk/0)調用tk/0({call,0,{f,4}}),然後調用get_toolkit/0({call,0,{f,2}})。使用get_toolkit/0的函數直接直接調用get_toolkit/0({call,0,{f,2}})。
所以,我認爲沒有優化應用。
而且,我做了一段時間的測試,其似乎支持這一假說;)
如果你的代碼因爲額外的函數調用而變慢,我會爭辯說你的代碼的其餘部分已經完全優化了。總之,沒關係。 –
我不知道,我沒有測量我的代碼的執行時間,這是更多關於知識和最佳實踐:) – niahoo
談到最佳實踐我認爲你應該使用全名(清晰度)。如果您發現它可以節省大量時間來使用快捷方式,那麼您總是可以自動替換它。 –