<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Thomas Lindgren wrote:
<blockquote
cite="mid20060131135824.64180.qmail@web34407.mail.mud.yahoo.com"
type="cite">
<pre wrap="">
--- Rudolph van Graan <a class="moz-txt-link-rfc2396E" href="mailto:rvg@patternmatched.com"><rvg@patternmatched.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hi Joe,
Here is one which I wrote some time ago. It does
work, but there
might be some issues. (No guarantees on speed!)
</pre>
</blockquote>
<pre wrap=""><!---->
Here is an easy speedup:
-define(CRC16Def, {...}).
crc_index(N) -> element(N+1, ?CRC16Def).
This is O(1) time rather than O(N) and 1/2 the space
of the original definition. Pretty good, but there's
more.
</pre>
</blockquote>
No, it is going to be O(N') (where N' is the size of the table), since
the structure ?CRC16Def will be built every time !<br>
At least that is the way it looks by looking at the -S output of the
compiler:<br>
<br>
<small><font face="Courier New, Courier, monospace">-module(stest).<br>
-compile(export_all).<br>
-define(t,{aa,bb,cc}).<br>
crc_index(N) -> element(N, ?t).<br>
</font></small><br>
<small><font face="Courier New, Courier, monospace">erlc -S stest.erl</font></small>
=====> <small><font face="Courier New, Courier, monospace">stest.S</font></small>
:<br>
....<br>
<small><font face="Courier New, Courier, monospace">{function,
crc_index, 1, 6}.<br>
{label,5}.<br>
{func_info,{atom,stest},{atom,crc_index},1}.<br>
{label,6}.<br>
{test_heap,4,1}.<br>
{put_tuple,3,{x,1}}.<br>
{put,{atom,aa}}.<br>
{put,{atom,bb}}.<br>
{put,{atom,cc}}.<br>
{bif,element,{f,0},[{x,0},{x,1}],{x,0}}.<br>
{'%live',1}.<br>
return.<br>
</font></small><br>
At least at this level of the code it looks like big structure
constants are built at every call anyway. <br>
The beaviour looks similar for lists.<br>
But perhaps some lower level of the compiler discovers this and puts
the constant in a constant area?<br>
<br>
-- Thomas<br>
<br>
</body>
</html>