Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

fastest wc program

Name: Anonymous 2013-08-07 20:47

Write the fastest program that counts the number of words and lines in a string.

Here's my solution

#include "stdlib.h"
#include "stdbool.h"
#include "stdio.h"

// macro that increments index, sets variables for the next stage, and
// gotos the label indexed by the current character
#define next(c, lc, iw) cnt = (c); lcnt = (lc); in_word = (iw); idx++; goto *(jmp_arr[(int)str[idx-1]]);

                             
int main(int argc, char** argv)
{
  /* test string */
  char* str = argv[1];
 
 
  /* normally this would be done as a sort of partial evaluation, but i can't for example generate an optimized function at runtime in C */
  void** jmp_arr;
 
  jmp_arr = malloc(sizeof(char)*256);
  int i;
  for (i = 0; i < 256; i++)
    {
      switch ((char)i)
        {
        case '\n':
          jmp_arr[i] = &&newline;
          break;
        case ' ':
          jmp_arr[i] = &&whitespace;
          break;
        default:
          jmp_arr[i] = &&character;
          break;
        }
    }
 
  // null character points to exit label
  jmp_arr[0] = &&terminate;

 
 
  int cnt = 0;
  int lcnt = 1;
  bool in_word = false;
  int idx = 0;
 
 // jump to label for first char
  goto *(jmp_arr[(int)str[0]]);
 
 terminate:
  printf("words: %i, lines: %i\n", cnt, lcnt);
  return 0;
 
 whitespace:
  next(cnt, lcnt, false);
   
 newline:
  next(cnt, lcnt+1, false);
 
 character:
  if (in_word)
    {
      next(cnt, lcnt, true);
    }
  else
    {
      next(cnt+1, lcnt, true);
    }
}

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 3:45

Your implementations are horrible.
use http://pastebin.com/rvaUsYGV

Name: Anonymous 2013-08-08 3:49

>>41

your code is shit, slow and doesn't work (it doesnt count anything lel)

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 3:57

minor update:replaced divide by shift, couple cycles saved.
http://pastebin.com/DqAwmPPL

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 4:00

About 7.2million cycles for 425kb file. >>42 can you write any version that is faster?

Name: Anonymous 2013-08-08 4:08

awk '{w += NF} END {print NR, w}' anus.txt

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 4:14

http://pastebin.com/NKne6UPZ v1.03 now at 7million cycles.

Name: Anonymous 2013-08-08 5:04

>>46
Your brilliance hasn't diminished one bit, frozen voidy.

Name: Anonymous 2013-08-08 5:33

>>30
Back to the imageboards, ``please''

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 5:53

http://pastebin.com/k72NkLpk Line count utility (complementary to wc.c)

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-08-08 6:19

>>49
[ocde]c=getfile(argv[1],&fsize);[/code]
You are an idiot.

Write the fastest program that counts the number of words and lines in a string.
You say "in a string" but not where it comes from, so you get just a function.

void countwl(unsigned char *s, unsigned int *pWords, unsigned int *pLines) {
 unsigned char c, *t = s, frist = *s, prevs = 1;
 unsigned int words = 0, lines = 0;
 while(c=*t++) {
  unsigned int n = !(c - '\n');
  unsigned int w = n || !(c - ' ') || !(c - '\t');
  lines += n;
  words += prevs & !w;
  prevs = w;
 }
 lines += !!frist;
 *pLines = lines;
 *pWords = words;
}

Name: Anonymous 2013-08-08 6:51

>>50
you forgot vertical tabs

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 6:56

Due popular demand countwords and countlines added to void.h
http://pastebin.com/hEE36CTc

Name: Anonymous 2013-08-08 7:15

FrozenVoid and Couldder interacting in a thread, all we need now is tdavis to stop making those "NEXT" threads and join in this one.

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 7:42

http://pastebin.com/Ja3RQ2rD void.h 1.47 now includes countnonspaceseq which returns number of "words"(sequences) which can contain any character which isn't defined as C whitespace.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-08-08 7:48

>>51
I can't say I've ever actually seen a text file with those in it.

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2013-08-08 8:08

http://pastebin.com/q1Dm2PBL v1.48 added generic countcharseq which can use a custom ascii table.

Name: Anonymous 2013-08-08 8:41

>>56

>shit indentation
>shit notation
>unreadable cancer code
>not oop
>not reusable at some point.
>bloated

fuck you frozen void.

Name: Anonymous 2013-08-08 8:52

>>57
Lets rewrite it in Python so it will become beautiful.

Name: Anonymous 2013-08-08 8:59

>>57
AFAIK FV considered unions and structs a form of OOP and refused to use them.

Name: Anonymous 2013-08-08 9:09

>>58

python is more shit lol

Name: Anonymous 2013-08-08 9:52

>>57
>le meme quotes
>le implyin/g/
>le quoting something that hasn't been said literally
>le reddit
>le 2013
>lel

fuck you /g/ shitstain.

Name: Anonymous 2013-08-08 11:12

PENIS

Name: Anonymous 2013-08-08 12:17

>>61
>
I suggest to leave here.

Name: sage 2013-08-08 12:42

Name: Anonymous 2013-08-08 12:51

>>63
But >>61 is the ironic shitposter, and >>57 is the actual shitposter!

Name: Anonymous 2013-08-08 13:01

>>64

People consider C to be a portable Assembler.  I consider them wrong because all CPU support a computed jump.

Name: Anonymous 2013-08-08 15:12

>>2
  void** jmp_arr;
 
  jmp_arr = malloc(sizeof(char)*256);

>implying void** and char are the same size
I shiggy diggy.

Name: Anonymous 2013-08-08 15:23

>>67
>>67
LLLLLLLLLLLLLLLLLLEEEEEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLLLL E/G/IN ISHI/G/G/Y>IMPLYIN/G/ MEME /G/RO XDDDDDDD THAT WAS E/G/IN FOR THE WIN XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD LELELELLLLLLLLLLLLLLLEEEEEEEEEEEEEEEEELELELELELELEL

>LE 2013
>NOT LE MEMEIN/G/ WITH HIS /G/ROS
XDDDDDDDDDDDDDDDDDDDDDD LE RE/G/G/IT FACE INSTALL LE /G/ENTOO ON LE REDDIT XDDDDDDDDDDDDD

Name: Anonymous 2013-08-08 16:33

>>67
my pc is 8-bit get off my case asshole

Name: Anonymous 2013-08-08 16:48

>>67
my anus is a textboard get off my board fa/g/g/ot

Name: Anonymous 2013-08-08 19:02

>>65
They are both shitposters.

Name: Anonymous 2013-08-08 19:37

>>56
that's fucking disgusting dude

Name: Anonymous 2013-08-08 23:56

>>67
You mean void* and char?

Name: Anonymous 2013-08-09 1:50

>>57,72
so this is the reaction of the new /g/ posters to frozenvoid.

Name: Anonymous 2013-08-09 12:24

>>74
Nice try kid but Frozenvoid is a newfag compared to me.

Name: Anonymous 2013-08-12 8:36

>>75
█████████████████████████████████████████████
█▒▒▒▒▓██████████▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▓█████████
█▒▒▒▒▓██████████▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▓█████████
█▒▒▒▒▓██████████▒▒▒▒▓██████████▒▒▒▒▓█████████
█▒▒▒▒▓██████████▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▓█████████
█▒▒▒▒▓██████████▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▓█████████
█▒▒▒▒▓██████████▒▒▒▒▓██████████▒▒▒▒▓█████████
█▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▒▒▒▒▒▒▒▒▓█
█▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▒▒▒▒▒▒▒▒▓██▒▒▒▒▒▒▒▒▒▒▒▒▓█
█████████████████████████████████████████████

Name: Anonymous 2013-08-14 19:00

>>76
check my doubles you fucking faggot

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List