[erlang-questions] count events within last XX minutes

Michael Truog mjtruog@REDACTED
Sun Aug 26 10:02:09 CEST 2012


There often is a habit with quick projects to throw data in ets, since it is easy to access the data as global data.  This helps people coming from an imperative programming background.  I don't see a good reason in the email thread that shows that ets is the best solution, simply because the amount of data is not clear.  ets can be used to limit the memory consumption, but so far, memory consumption was not mentioned as a concern.  Having an update_counter function doesn't sound too convincing because programmers are generally capable of a fetch-increment-store routine.

So, the moral of the story is to test and experiment.  You could try internal state with a dict as compared to ets, and see which make sense based on your requirements.

On 08/25/2012 11:33 PM, Max Bourinov wrote:
> Hi guys,
>
> Thank you guys for all your replies.
>
> I think I will go with my own implementation. The only thing I cannot understand, why you suggest using ETS for this? Why keeping data in the state is not ok? I think on heavy load the state approach will perform better than ETS. Moreover, I think ETS is overkill for this task.
>
> p.s. In my case XX minutes wont exceed 120.
>
> Best regards,
> Max
>
>
>
>
> On Sat, Aug 25, 2012 at 12:05 AM, Mike Oxford <moxford@REDACTED <mailto:moxford@REDACTED>> wrote:
>
>     If you care about, as you say, "certain minute" you can use Ulf's gproc.  If you want "last minute" then the ETS solution below will work well, with the modification on the Counter to use a ms/us timer like erlang:now().
>
>     -mox
>
>
>     On Fri, Aug 24, 2012 at 8:14 AM, Anders Nygren <anders.nygren@REDACTED <mailto:anders.nygren@REDACTED>> wrote:
>
>         Use an ETS table with {Counter,{YYYY,MM,DD,HH,MM}} as key, and
>         ets:update_counter/2,3.
>         update_counter returns the new counter value, so if it is 1, (or the
>         increment used) You know that a new minute has been entered so You can
>         delete the oldest.
>
>         /Anders
>
>         On Fri, Aug 24, 2012 at 9:16 AM, Max Bourinov <bourinov@REDACTED <mailto:bourinov@REDACTED>> wrote:
>         > Dear Erlangers,
>         >
>         > Does anybody know best memory efficient way to count events within last XX
>         > minutes?
>         >
>         > So far I have the following idea: For each counter I have a process. The
>         > counter process has a list of XX items. Each item represents a certain
>         > minute, so I always know where is my current counter. There is also must be
>         > a mechanism to remove last item from the list and add a new one. Summing
>         > values from all items is a number of events within last XX minutes. This is
>         > it.
>         >
>         > Maybe there is a ready lib that does the same in a better way?
>         >
>         > Any suggestions are welcome!
>         >
>         > Best regards,
>         > Max
>         >
>         >
>         >
>         > _______________________________________________
>         > erlang-questions mailing list
>         > erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>         > http://erlang.org/mailman/listinfo/erlang-questions
>         >
>         _______________________________________________
>         erlang-questions mailing list
>         erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>         http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120826/0f6f713d/attachment.htm>


More information about the erlang-questions mailing list