Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 95627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:41:16+00:00 2026-05-10T23:41:16+00:00

I was hanging out in my profiler for a while trying to figure out

  • 0

I was hanging out in my profiler for a while trying to figure out how to speed up a common log parser which was bottlenecked around the date parsing, and I tried various algorithms to speed things up.

The thing I tried that was fastest for me was also by far the most readable, but potentially non-standard C.

This worked quite well in GCC, icc, and my really old and picky SGI compiler. As it’s a quite readable optimization, where doesn’t it do what I want?

static int parseMonth(const char *input) {     int rv=-1;     int inputInt=0;     int i=0;      for(i=0; i<4 && input[i]; i++) {         inputInt = (inputInt << 8) | input[i];     }      switch(inputInt) {         case 'Jan/': rv=0; break;         case 'Feb/': rv=1; break;         case 'Mar/': rv=2; break;         case 'Apr/': rv=3; break;         case 'May/': rv=4; break;         case 'Jun/': rv=5; break;         case 'Jul/': rv=6; break;         case 'Aug/': rv=7; break;         case 'Sep/': rv=8; break;         case 'Oct/': rv=9; break;         case 'Nov/': rv=10; break;         case 'Dec/': rv=11; break;     }     return rv; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:41:17+00:00Added an answer on May 10, 2026 at 11:41 pm

    Solaris 10 – SPARC – SUN Compiler.

    Test code:

    #include <stdio.h>  static int parseMonth(const char *input) {     int rv=-1;     int inputInt=0;     int i=0;      for(i=0; i<4 && input[i]; i++) {         inputInt = (inputInt << 8) | input[i];     }      switch(inputInt) {         case 'Jan/': rv=0; break;         case 'Feb/': rv=1; break;         case 'Mar/': rv=2; break;         case 'Apr/': rv=3; break;         case 'May/': rv=4; break;         case 'Jun/': rv=5; break;         case 'Jul/': rv=6; break;         case 'Aug/': rv=7; break;         case 'Sep/': rv=8; break;         case 'Oct/': rv=9; break;         case 'Nov/': rv=10; break;         case 'Dec/': rv=11; break;     }      return rv; }  static const struct {     char *data;     int   result; } test_case[] = {     { 'Jan/', 0 },     { 'Feb/', 1 },     { 'Mar/', 2 },     { 'Apr/', 3 },     { 'May/', 4 },     { 'Jun/', 5 },     { 'Jul/', 6 },     { 'Aug/', 7 },     { 'Sep/', 8 },     { 'Oct/', 9 },     { 'Nov/', 10 },     { 'Dec/', 11 },     { 'aJ/n', -1 }, };  #define DIM(x) (sizeof(x)/sizeof(*(x)))  int main(void) {     size_t i;     int    result;      for (i = 0; i < DIM(test_case); i++)     {         result = parseMonth(test_case[i].data);         if (result != test_case[i].result)             printf('!! FAIL !! %s (got %d, wanted %d)\n',                    test_case[i].data, result, test_case[i].result);     }     return(0); } 

    Results (GCC 3.4.2 and Sun):

    $ gcc -O xx.c -o xx xx.c:14:14: warning: multi-character character constant xx.c:15:14: warning: multi-character character constant xx.c:16:14: warning: multi-character character constant xx.c:17:14: warning: multi-character character constant xx.c:18:14: warning: multi-character character constant xx.c:19:14: warning: multi-character character constant xx.c:20:14: warning: multi-character character constant xx.c:21:14: warning: multi-character character constant xx.c:22:14: warning: multi-character character constant xx.c:23:14: warning: multi-character character constant xx.c:24:14: warning: multi-character character constant xx.c:25:14: warning: multi-character character constant $ ./xx $ cc -o xx xx.c $ ./xx !! FAIL !! Jan/ (got -1, wanted 0) !! FAIL !! Feb/ (got -1, wanted 1) !! FAIL !! Mar/ (got -1, wanted 2) !! FAIL !! Apr/ (got -1, wanted 3) !! FAIL !! May/ (got -1, wanted 4) !! FAIL !! Jun/ (got -1, wanted 5) !! FAIL !! Jul/ (got -1, wanted 6) !! FAIL !! Aug/ (got -1, wanted 7) !! FAIL !! Sep/ (got -1, wanted 8) !! FAIL !! Oct/ (got -1, wanted 9) !! FAIL !! Nov/ (got -1, wanted 10) !! FAIL !! Dec/ (got -1, wanted 11) $ 

    Note that the last test case still passed – that is, it generated a -1.

    Here’s a revised – more verbose – version of parseMonth() which does work the same under both GCC and Sun C compiler:

    #include <stdio.h>  /* MONTH_CODE('Jan/') does not reduce to an integer constant */ #define MONTH_CODE(x)   ((((((x[0]<<8)|x[1])<<8)|x[2])<<8)|x[3])  #define MONTH_JAN       (((((('J'<<8)|'a')<<8)|'n')<<8)|'/') #define MONTH_FEB       (((((('F'<<8)|'e')<<8)|'b')<<8)|'/') #define MONTH_MAR       (((((('M'<<8)|'a')<<8)|'r')<<8)|'/') #define MONTH_APR       (((((('A'<<8)|'p')<<8)|'r')<<8)|'/') #define MONTH_MAY       (((((('M'<<8)|'a')<<8)|'y')<<8)|'/') #define MONTH_JUN       (((((('J'<<8)|'u')<<8)|'n')<<8)|'/') #define MONTH_JUL       (((((('J'<<8)|'u')<<8)|'l')<<8)|'/') #define MONTH_AUG       (((((('A'<<8)|'u')<<8)|'g')<<8)|'/') #define MONTH_SEP       (((((('S'<<8)|'e')<<8)|'p')<<8)|'/') #define MONTH_OCT       (((((('O'<<8)|'c')<<8)|'t')<<8)|'/') #define MONTH_NOV       (((((('N'<<8)|'o')<<8)|'v')<<8)|'/') #define MONTH_DEC       (((((('D'<<8)|'e')<<8)|'c')<<8)|'/')  static int parseMonth(const char *input) {     int rv=-1;     int inputInt=0;     int i=0;      for(i=0; i<4 && input[i]; i++) {         inputInt = (inputInt << 8) | input[i];     }      switch(inputInt) {         case MONTH_JAN: rv=0; break;         case MONTH_FEB: rv=1; break;         case MONTH_MAR: rv=2; break;         case MONTH_APR: rv=3; break;         case MONTH_MAY: rv=4; break;         case MONTH_JUN: rv=5; break;         case MONTH_JUL: rv=6; break;         case MONTH_AUG: rv=7; break;         case MONTH_SEP: rv=8; break;         case MONTH_OCT: rv=9; break;         case MONTH_NOV: rv=10; break;         case MONTH_DEC: rv=11; break;     }      return rv; }  static const struct {     char *data;     int   result; } test_case[] = {     { 'Jan/', 0 },     { 'Feb/', 1 },     { 'Mar/', 2 },     { 'Apr/', 3 },     { 'May/', 4 },     { 'Jun/', 5 },     { 'Jul/', 6 },     { 'Aug/', 7 },     { 'Sep/', 8 },     { 'Oct/', 9 },     { 'Nov/', 10 },     { 'Dec/', 11 },     { 'aJ/n', -1 },     { '/naJ', -1 }, };  #define DIM(x) (sizeof(x)/sizeof(*(x)))  int main(void) {     size_t i;     int    result;      for (i = 0; i < DIM(test_case); i++)     {         result = parseMonth(test_case[i].data);         if (result != test_case[i].result)             printf('!! FAIL !! %s (got %d, wanted %d)\n',                    test_case[i].data, result, test_case[i].result);     }     return(0); } 

    I wanted to use MONTH_CODE() but the compilers did not cooperate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer For that kind of access you should really be using… May 11, 2026 at 6:41 am
  • added an answer The previous two answers (by teun and Craig Bovis) are… May 11, 2026 at 6:41 am
  • added an answer Issue these commands on what used to be the remote:… May 11, 2026 at 6:41 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.