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


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

Re: My code is twice as fast as Owl's.

From owl <owl@rooftop.invalid>
Newsgroups comp.os.linux.advocacy
Subject Re: My code is twice as fast as Owl's.
Date 2017-08-03 03:12 +0000
Organization O.W.L.
Message-ID <szvbz003a.byu@rooftop.invalid> (permalink)
References <Jeff-Relf.Me@Aug.2--9.17am.Seattle.2017> <bb3c1e91-a8cd-43b5-9ba5-50876521a946@googlegroups.com> <szb03a.buu4@rooftop.invalid> <Jeff-Relf.Me@Aug.2--7.46pm.Seattle.2017>

Show all headers | View raw


Jeff-Relf.Me wrote:
> My code is twice as fast as Owl's:
> 
>   Me:  17.7 MicroSeconds
>   Owl: 32.3 MicroSeconds.
> 
> My code:
> 
>   double Mark = Secs ;  int i = 0 ; 
>   while( i++ < 100 )
>     i%3 ? ( i%5 ? Sh( L"%d", i ) : Sh( L"Buz"    ) )
>         :   i%5 ? Sh( L"Fiz"   ) : Sh( L"FizBuz" );
>    Mark = Secs - Mark, Sh( L"Me:  %sSeconds", Prn( Mark ) );
> 
> Owl's code ( for my console ):
> 
>   #define MAX 100
>   #define FOO 3
>   #define BAR 5
> 
>   void blah(int x){     Sh( L"%d",x);};
>   void fizz(int x){     Sh( L"Fiz");};
>   void buzz(int x){     Sh( L"Buz");};
>   void fizzbuzz(int x){ Sh( L"FizzBuz");};
> 
>     Mark = Secs ;
>     void (*f[])(int)={blah,fizz,buzz,fizzbuzz}; int i=0,j=0,v=0;
>     for(i=1;i<=MAX;i++) {
>        for(j=FOO;j<=i;j+=FOO) { if(j==i){v+=1;} }
>        for(j=BAR;j<=i;j+=BAR) { if(j==i){v+=2;} } f[v](i);  v=0;   }
>     Mark = Secs - Mark, Sh( L"Owl: %sSeconds", Prn( Mark ) );
> 
> My timer:
> 
>   #define  Tics ( QueryPerformanceCounter( ( DubInt * ) & _Tics ), _Tics )
>   #define  Secs ( _Secs = Tics / _Hz )
> 
>   typedef unsigned __int64  u64 ;  typedef LARGE_INTEGER  DubInt ;
>   u64  _Tics, Hz ;   double _Secs, _Hz ;
>   QueryPerformanceFrequency( ( DubInt * ) & Hz ), _Hz = Hz ;
> 
> P.S. No one would hire Fabian.  Per usual, he refuses to show any code;
> instead, he mocks the interviewer's task, and insults him.
> 

I fixed that code already.  Use this:

anon@lowtide:~/code/fizzbuzz$ cat ff.c
#include <stdio.h>

#define MAX 100000
#define FOO 3
#define BAR 5

void blah(int x){printf("%d\n",x);};
void fizz(int x){printf("Fizz\n");};
void buzz(int x){printf("Buzz\n");};
void fizzbuzz(int x){printf("FizzBuzz\n");};

int main(int argc, char *argv[])
{
   void (*f[])(int)={blah,fizz,buzz,fizzbuzz};
   int i=0,j=FOO,k=BAR,v=0;
   for(i=1;i<=MAX;i++)
   {
      for(;j<=i;j+=FOO)
      {
        if(j==i){v+=1;}
      }
      for(;k<=i;k+=BAR)
      {
        if(k==i){v+=2;}
      }
      f[v](i);
      v=0;
   }
   return 0;
}

anon@lowtide:~/code/fizzbuzz$

I get similar speed to yours (MAX is set to 100000).

anon@lowtide:~/code/fizzbuzz$ time ./relf >relfout

real    0m0.027s
user    0m0.028s
sys     0m0.000s
anon@lowtide:~/code/fizzbuzz$ time ./ff >ffout

real    0m0.027s
user    0m0.028s
sys     0m0.000s
anon@lowtide:~/code/fizzbuzz$ 

I'm using this for yours:

anon@lowtide:~/code/fizzbuzz$ cat relf.c
#include <stdio.h>

int main(void)
{
int i = 0 ;
  while( i++ < 100000 )
    i%3 ? ( i%5 ? printf( "%d\n", i ) : printf( "Buz\n"    ) )
        :   i%5 ? printf( "Fiz\n" )   : printf( "FizBuz\n" );
return 0;
}
anon@lowtide:~/code/fizzbuzz$ 


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


Thread

Re: FizzBuzz: an Interview Question ( for programmers ). fr314159@gmail.com - 2017-08-02 11:14 -0700
  Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-02 20:19 +0000
    Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-02 21:06 +0000
    Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-02 21:37 +0000
      Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-03 01:46 +0000
        Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-03 04:32 +0000
          Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-03 05:01 +0000
            Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-03 05:06 +0000
              Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-03 05:15 +0000
                Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 05:02 +0000
                Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-04 13:52 +0000
                Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 14:00 +0000
                Re: FizzBuzz: an Interview Question ( for programmers ). Marek Novotny <marek.novotny@marspolar.com> - 2017-08-04 12:56 -0500
                Re: FizzBuzz: an Interview Question ( for programmers ). Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 11:03 -0700
                Re: FizzBuzz: an Interview Question ( for programmers ). Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 18:05 +0000
                Re: FizzBuzz: an Interview Question ( for programmers ). Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 11:08 -0700
                Re: FizzBuzz: an Interview Question ( for programmers ). Snit <usenet@gallopinginsanity.com> - 2017-08-04 11:23 -0700
                Re: Marek Novotny is a command line jockey. Marek Novotny <marek.novotny@marspolar.com> - 2017-08-04 14:24 -0500
                Re: Marek Novotny is a command line jockey. Snit <usenet@gallopinginsanity.com> - 2017-08-04 12:45 -0700
                Re: This is better, no ? Marek Novotny <marek.novotny@marspolar.com> - 2017-08-04 15:24 -0500
                Re: Marek Novotny is a command line jockey. Snit <usenet@gallopinginsanity.com> - 2017-08-04 12:38 -0700
    Re: My code is twice as fast as Owl's. owl <owl@rooftop.invalid> - 2017-08-03 03:12 +0000
  Re: FizzBuzz: an Interview Question ( for programmers ). "F. Russell" <fr@random.info> - 2017-08-02 23:04 +0000
    Re: FizzBuzz: an Interview Question ( for programmers ). "F. Russell" <fr@random.info> - 2017-08-02 23:25 +0000
      Re: FizzBuzz: an Interview Question ( for programmers ). owl <owl@rooftop.invalid> - 2017-08-03 01:50 +0000
    Re: Fabian's code is always slower. owl <owl@rooftop.invalid> - 2017-08-03 04:41 +0000
      Re: Fabian's code is always slower. owl <owl@rooftop.invalid> - 2017-08-03 05:17 +0000
      Re: What's easier to read/maintain ? owl <owl@rooftop.invalid> - 2017-08-03 05:57 +0000
    Re: Fabian's code is always slower. fr314159@gmail.com - 2017-08-03 08:21 -0700
      Re: Fabian's code is always slower. Steve Carroll <fretwizzer@gmail.com> - 2017-08-03 09:24 -0700
      Re: Fabian's code is always slower. owl <owl@rooftop.invalid> - 2017-08-03 18:41 +0000
    Re: FizzBuzz: an Interview Question ( for programmers ). Snit <usenet@gallopinginsanity.com> - 2017-08-03 13:49 -0700
      Re: 4 lines, 4 states. Snit <usenet@gallopinginsanity.com> - 2017-08-03 22:11 -0700
        Re: 4 lines, 4 states. Snit <usenet@gallopinginsanity.com> - 2017-08-03 22:47 -0700
        Re: 4 lines, 4 states. Snit <usenet@gallopinginsanity.com> - 2017-08-03 22:52 -0700
          Re: Mozilla ThunderBird. Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 08:54 -0700
          Re: Mozilla ThunderBird. Snit <usenet@gallopinginsanity.com> - 2017-08-04 10:43 -0700
            Re: FizzBuzz is a trick question. Snit <usenet@gallopinginsanity.com> - 2017-08-04 11:47 -0700
              Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Snit <usenet@gallopinginsanity.com> - 2017-08-04 12:51 -0700
              Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. fr314159@gmail.com - 2017-08-04 12:57 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 13:02 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 20:02 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 13:11 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 20:17 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Steve Carroll <fretwizzer@gmail.com> - 2017-08-04 13:42 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. "F. Russell" <fr@random.info> - 2017-08-04 23:06 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Melzzzzz <Melzzzzz@zzzzz.com> - 2017-08-04 23:32 +0000
                Re: I'm the best c coder here. Snit <usenet@gallopinginsanity.com> - 2017-08-04 14:03 -0700
                Re: I'm the best c coder here. "F. Russell" <fr@random.info> - 2017-08-04 23:15 +0000
                Re: I'm the best c coder here. benj <benj@nobody.net> - 2017-08-04 19:35 -0400
                Re: I'm the best c coder here. "F. Russell" <fr@random.info> - 2017-08-05 16:08 +0000
                Re: Fabian seldom writes code, much less test it. owl <owl@rooftop.invalid> - 2017-08-05 03:09 +0000
                Re: 2 lines, 2^2 states, easily modified to 3^2 states. owl <owl@rooftop.invalid> - 2017-08-05 05:42 +0000
                Re: Who'd use "x++", in a loop, to find FizzBuzzGoozKaaz ? owl <owl@rooftop.invalid> - 2017-08-05 07:30 +0000
                Re: 2 lines, 2^2 states, easily modified to 3^2 states. Marek Novotny <marek.novotny@marspolar.com> - 2017-08-05 10:19 -0500
                Re: 2 lines, 2^2 states, easily modified to 3^2 states. Steve Carroll <fretwizzer@gmail.com> - 2017-08-05 10:13 -0700
                Re: 2 lines, 2^2 states, easily modified to 3^2 states. owl <owl@rooftop.invalid> - 2017-08-05 17:33 +0000
                Re: Fabian seldom writes code, much less test it. Marek Novotny <marek.novotny@marspolar.com> - 2017-08-04 22:24 -0500
                Re: Fabian seldom writes code, much less test it. "F. Russell" <fr@random.info> - 2017-08-05 13:37 +0000
                Re: Fabian seldom writes code, much less test it. "F. Russell" <fr@random.info> - 2017-08-05 14:25 +0000
                Re: I'm the best c coder here. owl <owl@rooftop.invalid> - 2017-08-05 03:22 +0000
                Re: I'm the best c coder here. Marek Novotny <marek.novotny@marspolar.com> - 2017-08-04 22:35 -0500
                Re: I'm the best c coder here. owl <owl@rooftop.invalid> - 2017-08-05 04:08 +0000
                Re: I reduced Owl's 43 lines to 5. owl <owl@rooftop.invalid> - 2017-08-05 07:25 +0000
                Re: Owl's FizBuzGozKaz branches 5 times more often. owl <owl@rooftop.invalid> - 2017-08-05 15:40 +0000
                Re: I'm _still_ the best c coder here. owl <owl@rooftop.invalid> - 2017-08-05 06:12 +0000
                Re: I'm _still_ the best c coder here. moroney@world.std.spaamtrap.com (Michael Moroney) - 2017-08-05 22:16 +0000
                Re: I increment things at the _start_ of a loop. Steve Carroll <fretwizzer@gmail.com> - 2017-08-06 11:04 -0700
                Re: I increment things at the _start_ of a loop. moroney@world.std.spaamtrap.com (Michael Moroney) - 2017-08-07 03:24 +0000
                Re: I increment things at the _start_ of a loop. Snit <usenet@gallopinginsanity.com> - 2017-08-06 20:35 -0700
                Re: "while ( x++ < 100 )" isn't "obscure". moroney@world.std.spaamtrap.com (Michael Moroney) - 2017-08-07 04:59 +0000
                Re: I'm the best c coder here. chrisv <chrisv@nospam.invalid> - 2017-08-07 06:59 -0500
                Re: I'm the best c coder here. Steve Carroll <fretwizzer@gmail.com> - 2017-08-08 10:23 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. DFS <nospam@dfs.com> - 2017-08-04 17:56 -0400
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. "F. Russell" <fr@random.info> - 2017-08-04 23:04 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Snit <usenet@gallopinginsanity.com> - 2017-08-04 16:09 -0700
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. fellatiohornblower@renlott.org - 2017-08-04 20:13 -0400
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. "F. Russell" <fr@random.info> - 2017-08-05 13:50 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. "F. Russell" <fr@random.info> - 2017-08-05 15:23 +0000
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Snit <usenet@gallopinginsanity.com> - 2017-08-05 10:06 -0700
              Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Doomsdrzej <.m@nsn.s> - 2017-08-05 11:52 -0400
                Re: Michael Glasser, Steve Carroll, Marek Novotny, Branimir Maksimovic, etc. Snit <usenet@gallopinginsanity.com> - 2017-08-05 09:55 -0700
    Re: Fabian's code is always slower. Takuya Saitoh <taka0038@gmail.com> - 2017-08-06 22:37 -0700

csiph-web