Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A look-up table <em>might</em> be faster:</p> <pre><code>char isvowel[UCHAR_MAX+1] = { 0 }; // or int instead of char, try both isvowel['a'] = 1; isvowel['e'] = 1; isvowel['i'] = 1; isvowel['o'] = 1; isvowel['u'] = 1; isvowel['A'] = 1; isvowel['E'] = 1; isvowel['I'] = 1; isvowel['O'] = 1; isvowel['U'] = 1; ... if (isvowel[(unsigned)name[i]]) vow_cnt++; </code></pre> <p>You could also try letting the compiler decide how to optimize, by using a switch:</p> <pre><code>switch(name[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++vow_cnt; default: } </code></pre> <p>Then again, either or both might be slower. You'd have to test it with more realistic data, since "optimizing" a program that only processes one short string is pretty futile. It will be effectively instantaneous whatever you do, so don't waste your own time. Performance only matters if you're processing a lot of data, so either one enormous string or lots of strings.</p> <p>ASCII has a trick, that <code>name[i]</code> is a particular letter (either case) if and only if <code>name[i] | 0x20</code> is that letter (lower case). You can use that to halve the number of case labels or (with care) the size of the lookup table. That may or may not affect performance.</p> <p>If you want to try a completely different approach, see how it performs, you can take the complicated logic and branching out of the loop:</p> <pre><code>size_t charcounts[UCHAR_MAX+1] = { 0 }; char *p = name; while (*p) { ++charcounts[(unsigned)*p]; ++p; } vow_cnt = charcounts['a'] + charcounts['e'] + ... + charcounts['U']; </code></pre> <p>This is likely to be slower for short strings, since the <code>charcounts</code> array has to be cleared for each string you test. It has a chance for large amounts of data, though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload