News for hackers. Stuff that matters.
Re:Six times faster than C by amm on Friday July 07, 2023 @05:34AM (#36629295)
Back-of-the-envelope approach that should eliminate most branching:

  int table[256] = {0};                                                           
                                                                                
  void init() {                                                                   
    table['s'] = 1;                                                             
    table['p'] = -1;                                                            
  }                                                                               
                                                                                
  int run_switches(char *input) {                                                 
    int res = 0;                                                                
    while (*input != '\0') res += table[*input++];                              
    return res;                                                                 
  }
Re:Six times faster than C by 414owen on Sunday July 09, 2023 @02:56PM (#36657666)
The array lookup approach taken in part two:

https://owen.cafe/posts/the-same-speed-as-c/

But taking the length of the string as a parameter is not, because that changes the problem statement (making the solution vectorizable)

Also note that you'll try to read element -1 of the input. You probably want to change the `>=` to a `>`