c++ - Is a letter compare slower than number compare? -


bear me here.

a couple months ago remember algorithms teacher discussing implementation of bucket sort (named distribution sort in algorithms book) , how works. basically, instead of taking number @ face value, start comparing binary representation so:

// 32 bit integers. input:  9 4  4: 00000000 00000000 00000000 00000110 9: 00000000 00000000 00000000 00001001 // etc. 

and start comparing right left.

// first step. 4: 0 9: 1  output: 9 4  // second step 4: 1 9: 0  output: 4 9 // technically stable algorithm, cannot observe here.  // third step  4: 1 9: 0  output: 4 9  // fourth step  4: 0 9: 1  output: 9 4 

and that's it; other 28 iterations zeroes, output won't change anymore. now, comparing whole bunch of strings go

// strings input: "christian" "denis"  christian: c h r s t n denis:     d e n s  // first step. christian: n denis:     s  output: christian, denis  // second step christian: denis:      output: denis, christian  // ...  

and forth.

my question is, comparing signed char, byte figure, faster comparing ints?

if had assume, 1 byte char compared faster 4-byte integer. correct? can make same assumption wchar_t, or utf-16/32 formats?

in c or c++, char one-byte integer (though "one byte" may or may not 8 bits). means in typical case, difference have deal whether single-byte comparison faster multi-byte comparison.

at least in cases, answer no. many risc processors don't have instructions deal single bytes @ all, operation on single byte carried out sign-extending byte word, operating on word, , (if necessary) masking bits outside of single byte zeros -- i.e., operating on whole word can around triple speed of operating on single byte.

even on x86 supports single-byte operations directly, they're still slower (on modern processor). there couple of things contribute this. first of all, instructions using registers of size "natural" current mode have simpler encoding instructions using other sizes. second, fair number of x86 processors have what's called "partial register stall" -- though it's implicit, internally risc does, carrying out operation on full-sized register, merging other bytes of original value. example, if produce result in al refer eax, sequence take longer execute if produced result in eax start with.

otoh, if @ old enough processors reverse (and was) true. extreme example, consider intel 8080 or zilog z80. both had 16-bit instructions, paths through alu 8 bits wide -- 16-bit addition, example, carried out 2 consecutive 8-bit additions. if 8-bit operation, twice fast. although 8-bit processors (distant) memory on desktop machines, they're still used in embedded applications, isn't entirely obsolete either.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -