Thread: A little optimization challenge.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Ok, as a start I'll begin with this (no compiler optimisations)

    Code:
       //=========== Start of challenge code =========
       
       int c;
       while((c = getc(f)) != EOF) {
            if (isalpha(c)) {
               int lc = tolower(c);
               int iv = lc == 'e' || lc == 'a' || lc == 'o' || lc == 'i' || lc == 'u';
               vowelCount += iv;
               conCount += !iv;
            }
       }
       //=========== End of challenge code =========
    The original code:

    Code:
    $./a.out 
    There are 2357034 consonants and 1433891 vowels in the Complete Works of William Shakespeare
    Time taken = 102125 microseconds
    My modified code:

    Code:
    $ ./a.out 
    There are 2357034 consonants and 1433891 vowels in the Complete Works of William Shakespeare
    Time taken = 32977 microseconds
    Only optimisation I made, really, is ordering the checks for a vowel based on English vowel frequency; i.e. I'm not concerned about much, right now, apart from how often vowels appear in typical English text. So, no real code changes but an improvement.

    Edit: So I guess I didn't optimise it at all. Or did I?

    Edit2: meh... compiler optimisations were turned off. My bad. My non-code-related changes are still faster with -O2 though.
    Last edited by Hodor; 10-02-2020 at 04:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need optimization
    By sc7 in forum C++ Programming
    Replies: 22
    Last Post: 11-10-2018, 11:11 AM
  2. Optimization challenge
    By Zacariaz in forum C Programming
    Replies: 1
    Last Post: 06-02-2015, 11:00 AM
  3. Optimization
    By lach in forum C Programming
    Replies: 4
    Last Post: 03-18-2006, 12:08 PM
  4. optimization
    By krithi in forum C Programming
    Replies: 9
    Last Post: 01-19-2003, 10:52 AM
  5. IDE optimization
    By Traveller in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-04-2002, 02:01 AM

Tags for this Thread