[Erlang] What are the pitfalls for future generations? (3) erlang Mining

Source: Internet
Author: User
Tags map data structure

[Erlang] What are the pitfalls for future generations? (3) erlang Mining

21.The following code is used to generate a random number:

random:seed(erlang:now()).

Erlang: now () is a continuously increasing number that never repeats, but is not suitable for Random Algorithms. It is best to use the following:

-spec get_random_num(integer()) -> integer().get_random_num(Max) ->    <<A:32, B:32, C:32>> = crypto:rand_bytes(12),    random:seed({A,B,C}),    random:uniform(Max).

22.What are the three digits of Pid <A, B, C>?

  • A, which node corresponds to (0 indicates A local node, and other numbers represent A remote node );
  • B. 15 bytes lower indicates the unique records of a process (the index of a process)
  • C, 16 ~ 18 bytes is also the unique count of the process (the same as B)

Http://stackoverflow.com/questions/243363/can-someone-explain-the-structure-of-a-pid-in-erlang

The verification is as follows:

Open a terminal test1

Open terminal test2

23.If the gen_server process does not need to process other messages for a long time, you can use the return value {noreply, hibernate} to enter the sleep state: Application Scenario:

For example, if you know that the process will not process any more messages after processing a specific message for a long time, you can set the returned message to enter the hibernate status!

This can reduce memory and cpu consumption.

24.Why is there a lot of good people who say that Record is so easy to use?

Disadvantages:

% 1. only atoms can be used as index elements; % 2. the structure is determined before compilation, and the index cannot be dynamically added. For example, if the person needs to add a phone attribute, record cannot be added.

So we have the Map data structure in the S, which can break through the above restrictions and truly Map.

Http://www.cnblogs.com/zhengsyao/p/erlang_map_brief.html

However, currently, mnesia only supports record. Map is not supported, so the Record upgrade will continue to exist in mneisa.

25The usage of after in. receive:

When a process is blocked, it is used to clear the process mailbox message,

receive  Msg ->           do_something(Msg);after 0 ->      do_something2)end.

The key is to see the after 0. Calling this function will use do_something (Msg) to consume the old messages of this process) (Of course, your do_something/1 will call this function recursively)

25.By default, the number of ETS is limited to 1400. You can use:

erl -env ERL_MAX_ETS_TABLES Number

To change the default value, but this restriction also prompts a very important message:Do not create an ets table for every process!

26.Do not connect to or monitor many processes across nodes:

If a node is disconnected due to network faults, all links or monitor processes will be immediately triggered, and a large amount of messages will be generated to a large number of processes, this puts a lot of burden on the system.

27.Use receive nesting to achieve the order of received messages:

receive       {first_deal_msg,Msg} ->                 do_someting(Msg),                  receive                        {second_deal_msg,Msg2} ->                              do_someting2(Msg2)                  endend.

28.Why is Erlang called Erlang? (Haha ):

Erlang

N.Erlan (Traffic Agency), busy hours;

A real language for communication...

29Simple use of. begin end statement blocks:
Problem description:
How to evenly divide a bunch of people into two groups at random?

Of course, the efficiency is not high,! Looks cool...

30. split_binary/2

It is usually more efficient to split a binary using matching instead of calling the split_binary/2 function. Furthermore, mixing bit syntax matching and split_binary/2 may prevent some optimizations of bit syntax matching.

It is more efficient to use the bit syntax to separate binary data than to use split_binary/2. Furthermore, the mixed use of the bit syntax and split_binary/2 will get the compiler sb (no related optimization tasks will be performed)

DO

<<Bin1:Num/binary,Bin2/binary>> = Bin,

DO NOT

{Bin1,Bin2} = split_binary(Bin, Num)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.