# Old style vs. new style boolean expressions

Sebastian Strollo <>
Tue Oct 8 22:38:19 CEST 2002


James Hague <> writes:

> > I don't think this make much of a difference most of the 
> > time, and even if it
> > increases the size noticeably - does it matter ?
> 
> It only matters to people who obsessively peek behind the scenes :)

For those that are curious there is always the 'S' option to the
compiler - it outputs the beam "assembly" into a "module.S" file. So
for example the following:

    -module(f).
    -compile(export_all).

    f1(N) when N >= $A, N =< $Z -> true;
    f1(N) when N >= $a, N =< $z -> true;
    f1(N) when N >= $0, N =< $9 -> true;
    f1($_) -> true;
    f1(_) -> false.

    f2(N) ->
            ((N >= $A) and (N =< $Z)) or
            ((N >= $a) and (N =< $z)) or
            ((N >= $0) and (N =< $9)) or
            (N == $_).

(I only have R7 at hand, so no andalso, orelse). And run the compiler
on that, like this:

    1> compile:file(f, ['S']).
    {ok,f}

Now you will have a file called f.S, and looking at f1 and f2 you will
see that it generates very different code indeed...

    {function, f1, 1, 2}.
      {label,1}.
        {func_info,{atom,f},{atom,f1},1}.
      {label,2}.
        {test,is_ge,{f,5},{x,0},{integer,65}}.
        {test,is_ge,{f,5},{integer,90},{x,0}}.
        {move,{atom,true},{x,0}}.
        {'%live',1}.
        return.
      {label,5}.
        {test,is_ge,{f,6},{x,0},{integer,97}}.
        {test,is_ge,{f,6},{integer,122},{x,0}}.
        {move,{atom,true},{x,0}}.
        {'%live',1}.
        return.
      {label,6}.
        {test,is_ge,{f,4},{x,0},{integer,48}}.
        {test,is_ge,{f,4},{integer,57},{x,0}}.
        {move,{atom,true},{x,0}}.
        {'%live',1}.
        return.
      {label,4}.
        {test,is_eq_exact,{f,7},{x,0},{integer,95}}.
        {move,{atom,true},{x,0}}.
        {'%live',1}.
        return.
      {label,7}.
        {move,{atom,false},{x,0}}.
        {'%live',1}.
        return.
    
    {function, f2, 1, 10}.
      {label,9}.
        {func_info,{atom,f},{atom,f2},1}.
      {label,10}.
        {bif,'>=',{f,0},[{x,0},{integer,65}],{x,1}}.
        {bif,'=<',{f,0},[{x,0},{integer,90}],{x,2}}.
        {bif,'and',{f,0},[{x,1},{x,2}],{x,1}}.
        {bif,'>=',{f,0},[{x,0},{integer,97}],{x,2}}.
        {bif,'=<',{f,0},[{x,0},{integer,122}],{x,3}}.
        {bif,'and',{f,0},[{x,2},{x,3}],{x,2}}.
        {bif,'or',{f,0},[{x,1},{x,2}],{x,1}}.
        {bif,'>=',{f,0},[{x,0},{integer,48}],{x,2}}.
        {bif,'=<',{f,0},[{x,0},{integer,57}],{x,3}}.
        {bif,'and',{f,0},[{x,2},{x,3}],{x,2}}.
        {bif,'or',{f,0},[{x,1},{x,2}],{x,1}}.
        {bif,'==',{f,0},[{x,0},{integer,95}],{x,2}}.
        {bif,'or',{f,0},[{x,1},{x,2}],{x,0}}.
        {'%live',1}.
        return.


Cheers,

/Sebastian



More information about the erlang-questions mailing list