Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.advocacy > #465444

Re: The substantiation that -highhorse asked for

From owl <owl@rooftop.invalid>
Newsgroups comp.os.linux.advocacy
Subject Re: The substantiation that -highhorse asked for
Date 2018-08-31 12:33 +0000
Organization O.W.L.
Message-ID <z8b003a.ba9o3g@rooftop.invalid> (permalink)
References (8 earlier) <Y8ohD.699$HK2.581@fx08.iad> <a9b00a0b3.e@rooftop.invalid> <JgxhD.7$UI2.1@fx18.iad> <g89z0b083.a44@rooftop.invalid> <f045ebee-7115-4ccc-8573-89e47422d3d0@googlegroups.com>

Show all headers | View raw


Steve Carroll <fretwizzer@gmail.com> wrote:
> On Wednesday, August 29, 2018 at 8:31:19 AM UTC-6, owl wrote:
>> DFS <nospam@nospam.com> wrote:
>> > On 8/29/2018 2:14 AM, owl wrote:
>> >> DFS <nospam@nospam.com> wrote:
>> >>>
>> >>> import random
>> >>>
>> >>> print "Group 1: r<0"
>> >>> print "Group 2: r==0"
>> >>> print "Group 3: r>0 && r<=10"
>> >>> print "Group 4: r>10 && r<=100"
>> >>> print "Group 5: r>100 && r<=1000"
>> >>> print "Group 6: r>1000 && r<=10000"
>> >>> print "Group 7: r>10000 && r<=100000"
>> >>> print "Group 8: r>100000 && r<=1000000"
>> >>> print "Group 9: r>1000000"
>> >>>
>> >>> def printgroup(grp):
>> >>>         print "Found random number " + str(r) + " in Group " + str(grp)
>> >>>
>> >>> for j in range(20):
>> >>>         r = random.randint(-1000,1001)
>> >>>         if r < 0:               printgroup(1); exit
>> >>>         if r > 1000000: printgroup(9); exit
>> >>>         comps =
>> >>> [(-1,0),(1,10),(11,100),(101,1000),(1001,10000),(10001,100000),(100001,1000000)]
>> >>>         for i, x in enumerate(range(len(comps))):
>> >>>                 if r > comps[i][0] and r <= comps[i][1]: printgroup(i+2); exit
>> >>>                 i+=1
>> >>>
>> >>>
>> >>>
>> >>> That's 3 if-then-exits to handle 9 comparisons
>> >> 
>> >> If you call that 3 ifs, then I get to call this zero ifs:
>> >> 
>> >> anon@lowtide:~/code/ifs$ cat goo.c
>> >> #include <stdio.h>
>> >> #include <stdlib.h>
>> >> 
>> >> void flz(void){printf("a<0\n");}
>> >> void fgm(void){printf("a>1000000\n");}
>> >> void f0(void){printf("a==0\n");}
>> >> void f1(void){printf("a>0 && a<=10\n");}
>> >> void f2(void){printf("a>10 && a<=100\n");}
>> >> void f3(void){printf("a>100 && a<=1000\n");}
>> >> void f4(void){printf("a>1000 && a<=10000\n");}
>> >> void f5(void){printf("a>10000 && a<=100000\n");}
>> >> void f6(void){printf("a>100000 && a<=1000000\n");}
>> >> 
>> >> int main(int argc, char *argv[])
>> >> {
>> >>     int a=0;
>> >>     int i=0;
>> >>     void (*fp[1000001])(void)={0};
>> >>     fp[0]=f0;
>> >>     for(i=1;i<=10;i++) fp[i]=f1;
>> >>     for(i=11;i<=100;i++) fp[i]=f2;
>> >>     for(i=101;i<=1000;i++) fp[i]=f3;
>> >>     for(i=1001;i<=10000;i++) fp[i]=f4;
>> >>     for(i=10001;i<=100000;i++) fp[i]=f5;
>> >>     for(i=100001;i<=1000000;i++) fp[i]=f6;
>> >>     a=atoi(argv[1]);
>> >>     (a<0?flz:a>1000000?fgm:fp[a])();
>> >>     return 0;
>> >> }
>> >> anon@lowtide:~/code/ifs$
>> > 
>> > affirmative
>> > 
>> 
>> But who would write it that way, and why?  Sure, there's no if statements,
>> but that comes at the expense of 1,000,001 function pointers.  And the
>> nested ternaries constitute conditional tests anyway.  Not to mention
>> less readability.  It would run faster, use less stack, and be much more
>> readable and faster to design and write without error, by just using a
>> list of ifs.
>> 
>> anon@lowtide:~/code/ifs$ time ./blah 500
>> a>100 && a<=1000
>> 
>> real    0m0.002s
>> user    0m0.000s
>> sys     0m0.000s
>> anon@lowtide:~/code/ifs$ time ./goo 500
>> a>100 && a<=1000
>> 
>> real    0m0.020s
>> user    0m0.008s
>> sys     0m0.008s
>> anon@lowtide:~/code/ifs$
> 
> What are those times representing, your if/else vs the above (or his code)?
> 

if/else vs the above.
But you can combine the above with DFS's loop and it's just as fast.

anon@lowtide:~/code/ifs$ cat qoo.c
#include <stdio.h>
#include <stdlib.h>

void flz(void){printf("a<0\n");}
void f0(void){printf("a==0\n");}
void f1(void){printf("a>0 && a<=10\n");}
void f2(void){printf("a>10 && a<=100\n");}
void f3(void){printf("a>100 && a<=1000\n");}
void f4(void){printf("a>1000 && a<=10000\n");}
void f5(void){printf("a>10000 && a<=100000\n");}
void f6(void){printf("a>100000 && a<=1000000\n");}
void fgm(void){printf("a>1000000\n");}

int main(int argc, char *argv[])
{
  int i=0;
  int a=0;
  void (*fp[])(void)={flz,f0,f1,f2,f3,f4,f5,f6,fgm};
  int comps[][2]={{-1,0},{0,10},{10,100},{100,1000},
                 {1000,10000},{10000,100000},{100000,1000000}};
  int arrsize=0;
  if(argc!=2)
  {
    fprintf(stderr,"feed me a number\n");
    exit(1);
  }
  arrsize=sizeof(comps)/sizeof(comps[0]);
  a=atoi(argv[1]);
  if (a<0){(fp[0])();}
  else if (a>1000000){(fp[8])();}
  else
  {
    for(i=0;i<arrsize;i++)
    { 
      if((a>comps[i][0]) && (a<=comps[i][1]))
      {
         (fp[i+1])();
         break;
      }
    }
  }
  return 0;
}
anon@lowtide:~/code/ifs$ 
anon@lowtide:~/code/ifs$ time ./qoo 500
a>100 && a<=1000

real    0m0.002s
user    0m0.000s
sys     0m0.000s
anon@lowtide:~/code/ifs$ 



> JS doesn't have 'range' (like Python) and it's not as fast as C but we
> do have 'map' (as does Python, not sure about C?) so if there are no
> edge cases it's easy to write (especially when nested). Non-nested: 
> 

You have to roll your own in C. 

Back to comp.os.linux.advocacy | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-28 08:28 -0400
  Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 09:20 -0700
    Re: The substantiation that -highhorse asked for fr314159@gmail.com - 2018-08-28 11:27 -0700
      Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 11:39 -0700
      Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-29 09:46 -0400
    Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-28 14:52 -0400
      Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 12:20 -0700
  Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 17:21 +0000
    Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-28 14:46 -0400
      Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 19:14 +0000
        Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 12:30 -0700
          Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 20:09 +0000
            Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 13:58 -0700
              Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 21:16 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 14:31 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 21:43 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 15:06 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 22:26 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-28 15:35 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-28 23:14 +0000
                Re: The substantiation that -highhorse asked for Snit <usenet@gallopinginsanity.com> - 2018-08-28 14:57 -0700
            Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-28 23:11 -0400
              Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-29 06:14 +0000
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-29 09:34 -0400
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-29 14:31 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-29 11:01 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-31 12:33 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-31 09:16 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-31 16:53 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-31 10:22 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-31 17:53 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-08-31 11:36 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 13:45 +0000
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 13:59 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-09-01 08:15 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 16:00 +0000
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-09-01 11:20 -0700
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-31 15:00 -0400
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-31 19:40 +0000
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-09-01 10:15 -0400
                Re: The substantiation that -highhorse asked for Steve Carroll <fretwizzer@gmail.com> - 2018-09-01 08:22 -0700
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 16:28 +0000
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-09-01 16:39 -0400
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 22:00 +0000
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-09-01 18:27 -0400
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 22:40 +0000
                Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-09-01 19:09 -0400
                Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-09-01 23:59 +0000
        Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-28 23:01 -0400
          Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-29 06:26 +0000
  Re: The substantiation that -highhorse asked for "F. Russell" <fr@random.info> - 2018-08-28 19:25 +0000
    Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-29 09:32 -0400
      Re: The substantiation that -highhorse asked for fr314159@gmail.com - 2018-08-29 06:46 -0700
        Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-08-29 09:55 -0400
          Re: The substantiation that -highhorse asked for fr314159@gmail.com - 2018-08-29 08:02 -0700
            Re: The substantiation that -highhorse asked for DFS <nospam@nospam.com> - 2018-09-01 10:14 -0400
          Re: The substantiation that -highhorse asked for owl <owl@rooftop.invalid> - 2018-08-29 15:12 +0000

csiph-web