From 5a082a0aa1a1dc2ef10180460efa5a51dfcce66a Mon Sep 17 00:00:00 2001 From: jixiuf Date: Fri, 23 Nov 2012 10:23:52 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=BA=AA=E7=A7=80=E5=B3=B0]:bin2hex=20?= =?UTF-8?q?=E5=8F=97=E5=BD=B1=E5=93=8D=E7=9A=84=E6=96=87=E4=BB=B6:=20=20?= =?UTF-8?q?=20=20=20bin=5Fto=5Fhex/src/readme.org=20=20=20=20=20bin=5Fto?= =?UTF-8?q?=5Fhex/src/hello.erl=20=20=20=20=20bin=5Fto=5Fhex/src/bin=5Fto?= =?UTF-8?q?=5Fhex.erl=20=20=20=20=20bin=5Fto=5Fhex/rebar.config=20=20=20?= =?UTF-8?q?=20=20bin=5Fto=5Fhex/Makefile=20=20=20=20=20bin=5Fto=5Fhex/Emak?= =?UTF-8?q?efile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin_to_hex/Emakefile | 2 + bin_to_hex/Makefile | 42 +++++++++++++++++++ bin_to_hex/rebar.config | 8 ++++ bin_to_hex/src/bin_to_hex.erl | 76 +++++++++++++++++++++++++++++++++++ bin_to_hex/src/hello.erl | 19 +++++++++ bin_to_hex/src/readme.org | 3 ++ 6 files changed, 150 insertions(+) create mode 100644 bin_to_hex/Emakefile create mode 100644 bin_to_hex/Makefile create mode 100644 bin_to_hex/rebar.config create mode 100644 bin_to_hex/src/bin_to_hex.erl create mode 100644 bin_to_hex/src/hello.erl create mode 100644 bin_to_hex/src/readme.org diff --git a/bin_to_hex/Emakefile b/bin_to_hex/Emakefile new file mode 100644 index 0000000..969ba55 --- /dev/null +++ b/bin_to_hex/Emakefile @@ -0,0 +1,2 @@ +{ 'src/*', [debug_info, {i ,"include/"} , {i ,"deps/"} ,{outdir,"ebin/"}] }. +{ 'test/*', [debug_info, {i ,"src/"}, {i ,"deps/"},{i ,"include/"}, {outdir,"ebin"} ] }. diff --git a/bin_to_hex/Makefile b/bin_to_hex/Makefile new file mode 100644 index 0000000..a3d5090 --- /dev/null +++ b/bin_to_hex/Makefile @@ -0,0 +1,42 @@ +PROJECT=bin_to_hex +PREVIOUS_RELEASE_VERSION=0.1PREFIX:=../ +DEST:=$(PREFIX)$(PROJECT) +REBAR=./rebar +REBAR_UP=../rebar +.PHONY:test + +compile: + @$(REBAR) get-deps compile + @-cp src/$(PROJECT).appup.src ebin/$(PROJECT).appup +edoc: + @$(REBAR) doc +test: + @rm -rf .eunit + @mkdir -p .eunit + @$(REBAR) skip_deps=true eunit +clean: + @$(REBAR) clean +build_plt: + @$(REBAR) build-plt +dialyzer: + @$(REBAR) dialyze +app: + @$(REBAR) create-app dest=$(DEST) appid=$(PROJECT) + +createnode: + @-mkdir rel + cd rel && $(REBAR_UP) create-node nodeid=$(PROJECT) +create-node:createnode +cn:createnode + +generate: + $(REBAR) generate +gen:generate +generatef: + $(REBAR) generate -f + +appup: + $(REBAR) generate-appups previous_release=$(PROJECT)-$(PREVIOUS_RELEASE_VERSION) +upgrade: + $(REBAR) generate-upgrade previous_release=$(PROJECT)-$(PREVIOUS_RELEASE_VERSION) +up:appup upgrade diff --git a/bin_to_hex/rebar.config b/bin_to_hex/rebar.config new file mode 100644 index 0000000..10bea2e --- /dev/null +++ b/bin_to_hex/rebar.config @@ -0,0 +1,8 @@ +%% -*- erlang -*- +%% {erl_opts, [debug_info,{src_dirs, ['src/']}, nowarn_unused_vars, nowarn_unused_function,native,{hipe,[o3]}]}. +{erl_opts, [debug_info]}. +{sub_dirs, ["rel"]}. +{lib_dirs,[".."]}. +{deps, [ ]}. +{cover_enabled, true}. +{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. diff --git a/bin_to_hex/src/bin_to_hex.erl b/bin_to_hex/src/bin_to_hex.erl new file mode 100644 index 0000000..e273098 --- /dev/null +++ b/bin_to_hex/src/bin_to_hex.erl @@ -0,0 +1,76 @@ +%%%------------------------------------------------------------------- +%%% @author 纪秀峰 +%%% @doc +%%% +%%% @end +%%% Created : 2012-11-23 10:12 by 纪秀峰 +%% Last Updated: 纪秀峰 2012-11-23 10:22:11 星期五 +%%%------------------------------------------------------------------- + + +%% timer:tc(fun()->lists:foreach(fun(L)-> bin_to_hex:bin_to_hex(term_to_binary(atom)) end, lists:seq(0,1000000))end). +-module(bin_to_hex). + +-compile([native, {hipe, [o3]}]). + +-export([bin_to_hex/1]). + +bin_to_hex(B) when is_binary(B) -> + bin_to_hex(B, <<>>). + +-define(H(X), (hex(X)):16). + +bin_to_hex(<<>>, Acc) -> Acc; +bin_to_hex(Bin, Acc) when byte_size(Bin) band 7 =:= 0 -> + bin_to_hex_(Bin, Acc); +bin_to_hex(<>, Acc) -> + bin_to_hex(Rest, <>). + +bin_to_hex_(<<>>, Acc) -> Acc; +bin_to_hex_(<>, Acc) -> + bin_to_hex_( + Rest, + <>). + +-compile({inline, [hex/1]}). + +hex(X) -> + element( + X+1, {16#3030, 16#3031, 16#3032, 16#3033, 16#3034, 16#3035, 16#3036, + 16#3037, 16#3038, 16#3039, 16#3041, 16#3042, 16#3043, 16#3044, + 16#3045, 16#3046, 16#3130, 16#3131, 16#3132, 16#3133, 16#3134, + 16#3135, 16#3136, 16#3137, 16#3138, 16#3139, 16#3141, 16#3142, + 16#3143, 16#3144, 16#3145, 16#3146, 16#3230, 16#3231, 16#3232, + 16#3233, 16#3234, 16#3235, 16#3236, 16#3237, 16#3238, 16#3239, + 16#3241, 16#3242, 16#3243, 16#3244, 16#3245, 16#3246, 16#3330, + 16#3331, 16#3332, 16#3333, 16#3334, 16#3335, 16#3336, 16#3337, + 16#3338, 16#3339, 16#3341, 16#3342, 16#3343, 16#3344, 16#3345, + 16#3346, 16#3430, 16#3431, 16#3432, 16#3433, 16#3434, 16#3435, + 16#3436, 16#3437, 16#3438, 16#3439, 16#3441, 16#3442, 16#3443, + 16#3444, 16#3445, 16#3446, 16#3530, 16#3531, 16#3532, 16#3533, + 16#3534, 16#3535, 16#3536, 16#3537, 16#3538, 16#3539, 16#3541, + 16#3542, 16#3543, 16#3544, 16#3545, 16#3546, 16#3630, 16#3631, + 16#3632, 16#3633, 16#3634, 16#3635, 16#3636, 16#3637, 16#3638, + 16#3639, 16#3641, 16#3642, 16#3643, 16#3644, 16#3645, 16#3646, + 16#3730, 16#3731, 16#3732, 16#3733, 16#3734, 16#3735, 16#3736, + 16#3737, 16#3738, 16#3739, 16#3741, 16#3742, 16#3743, 16#3744, + 16#3745, 16#3746, 16#3830, 16#3831, 16#3832, 16#3833, 16#3834, + 16#3835, 16#3836, 16#3837, 16#3838, 16#3839, 16#3841, 16#3842, + 16#3843, 16#3844, 16#3845, 16#3846, 16#3930, 16#3931, 16#3932, + 16#3933, 16#3934, 16#3935, 16#3936, 16#3937, 16#3938, 16#3939, + 16#3941, 16#3942, 16#3943, 16#3944, 16#3945, 16#3946, 16#4130, + 16#4131, 16#4132, 16#4133, 16#4134, 16#4135, 16#4136, 16#4137, + 16#4138, 16#4139, 16#4141, 16#4142, 16#4143, 16#4144, 16#4145, + 16#4146, 16#4230, 16#4231, 16#4232, 16#4233, 16#4234, 16#4235, + 16#4236, 16#4237, 16#4238, 16#4239, 16#4241, 16#4242, 16#4243, + 16#4244, 16#4245, 16#4246, 16#4330, 16#4331, 16#4332, 16#4333, + 16#4334, 16#4335, 16#4336, 16#4337, 16#4338, 16#4339, 16#4341, + 16#4342, 16#4343, 16#4344, 16#4345, 16#4346, 16#4430, 16#4431, + 16#4432, 16#4433, 16#4434, 16#4435, 16#4436, 16#4437, 16#4438, + 16#4439, 16#4441, 16#4442, 16#4443, 16#4444, 16#4445, 16#4446, + 16#4530, 16#4531, 16#4532, 16#4533, 16#4534, 16#4535, 16#4536, + 16#4537, 16#4538, 16#4539, 16#4541, 16#4542, 16#4543, 16#4544, + 16#4545, 16#4546, 16#4630, 16#4631, 16#4632, 16#4633, 16#4634, + 16#4635, 16#4636, 16#4637, 16#4638, 16#4639, 16#4641, 16#4642, + 16#4643, 16#4644, 16#4645, 16#4646}). diff --git a/bin_to_hex/src/hello.erl b/bin_to_hex/src/hello.erl new file mode 100644 index 0000000..00fff1a --- /dev/null +++ b/bin_to_hex/src/hello.erl @@ -0,0 +1,19 @@ +% Use: erlc hello.erl && erl -pa ./ebin -s hello run -s init stop -noshell + +-module(hello). +-export([bin_to_hex/1]). + +bin_to_hex(Bin)-> + bin_to_hex(Bin,[]). +%% erlang:integer_to_list +bin_to_hex(<<>>,Value)-> + lists:reverse(Value); +bin_to_hex(<>,Value)-> + Hex=integer_to_list(I,16), + NewHex=case length(Hex) of + 1-> + "0"++Hex; + _-> + Hex + end, + bin_to_hex(Bin,[NewHex|Value]). diff --git a/bin_to_hex/src/readme.org b/bin_to_hex/src/readme.org new file mode 100644 index 0000000..50007e9 --- /dev/null +++ b/bin_to_hex/src/readme.org @@ -0,0 +1,3 @@ +%% timer:tc(fun()->lists:foreach(fun(L)-> bin_to_hex:bin_to_hex(term_to_binary(atom)) end, lists:seq(0,1000000))end). +%% timer:tc(fun()->lists:foreach(fun(L)-> hello:binary_to_hex(term_to_binary(atom)) end, lists:seq(0,1000000))end). +结果显示 bin_to_hex模块里的方法 要快一些