-module(test). -export([test_string_contact/1, test_list_contact/1, test_list_contact2/1, string_contact/0, list_contact/0, list_contact2/0]). test_string_contact(N) -> statistics(runtime), statistics(wall_clock), %%loop(fun string_contact/0, N), string_contact(N), {_, Time1} = statistics(runtime), {_, Time2} = statistics(wall_clock), U1 = Time1 * 1000 /N, U2 = Time2 * 1000/N, io:format("string++ time: ~p/~p micro-seconds (runtime/wallclock)~n", [U1, U2]). loop(_Fun, 0) -> ok; loop(Fun, N) -> Fun(), loop(Fun, N-1). string_contact(0) -> ok; string_contact(N) -> string_contact(), string_contact(N-1). string_contact() -> L1 = "INVITE sip:1000@192.168.1.3 SIP/2.0\r\n", L2 = "From: sip:1000@192.168.1.3\r\n", L3 = "To: sip:bob@192.168.1.3\r\n", L4 = "Via: SIP/2.0/UDP server.biloxi.com;branch=z9hgbK4bawer\r\n", L5 = "Max-Forwards: 70\r\n", L6 = "Call-ID: a84b4c76e66710\r\n", L7 = "CSeq: 314519 INVITE\r\n", L8 = "Contact: \r\n", L9 = "Content-TYpe: application/sdp\r\n", L10 = "Content-Length: 0\r\n", L1 ++ L2 ++ L3 ++ L4 ++ L5 ++ L6 ++ L7 ++ L8 ++ L9 ++ L10. test_list_contact(N) -> statistics(runtime), statistics(wall_clock), %%loop(fun list_contact/0, N), list_contact(N), {_, Time1} = statistics(runtime), {_, Time2} = statistics(wall_clock), U1 = Time1 * 1000 /N, U2 = Time2 * 1000/N, io:format("list contact time: ~p/~p micro-seconds (runtime/wallclock)~n", [U1, U2]). list_contact(0) -> ok; list_contact(N) -> list_contact(), list_contact(N-1). list_contact() -> L1 = "INVITE sip:1000@192.168.1.3 SIP/2.0\r\n", L2 = "From: sip:1000@192.168.1.3\r\n", L3 = "To: sip:bob@192.168.1.3\r\n", L4 = "Via: SIP/2.0/UDP server.biloxi.com;branch=z9hgbK4bawer\r\n", L5 = "Max-Forwards: 70\r\n", L6 = "Call-ID: a84b4c76e66710\r\n", L7 = "CSeq: 314519 INVITE\r\n", L8 = "Contact: \r\n", L9 = "Content-TYpe: application/sdp\r\n", L10 = "Content-Length: 0\r\n", R1 = [L1], R2 = [L2 | R1], R3 = [L3 | R2], R4 = [L4 | R3], R5 = [L5 | R4], R6 = [L6 | R5], R7 = [L7 | R6], R8 = [L8 | R7], R9 = [L9 | R8], R10 = [L10 | R9], lists:reverse(R10). test_list_contact2(N) -> statistics(runtime), statistics(wall_clock), %%loop(fun list_contact2/0, N), list_contact2(N), {_, Time1} = statistics(runtime), {_, Time2} = statistics(wall_clock), U1 = Time1 * 1000 /N, U2 = Time2 * 1000/N, io:format("list contact time: ~p/~p micro-seconds (runtime/wallclock)~n", [U1, U2]). list_contact2(0) -> ok; list_contact2(N) -> list_contact2(), list_contact2(N-1). list_contact2() -> lists:flatten(list_contact()). %%%% NOTE: %%%% 1. For short message like above, string_contact with ++ is even faster than%%%% list_contact with list operation. %%%% 2. lists:flatten() is very heavy. test_list_contact is way faster than tes %%%% t_list_contact2.