c++ - Always assign vs check equality and assign -


is there noticeable performance difference between:

if (a != b) = b; , a = b;

when a , b both of same built-in type int or bool or maybe simple , small struct?

as understand second expression write memory every time (which assume heavier operation read), while first 1 if a , b not equal.

or depends on how b value changed?

i understand it's more of "++i vs i++" kind of question, curious though

it depends.

for x86 cpus, cost of operations involved in program, follows:

  • non-cached read (i.e. read ram not cached yet): ~100 clocks
  • cached read: 3 ~10 clocks
  • register read: 1/2 clock (value rough, there no such single operation "read")
  • write: varies , depends, ~1 clock
  • comparison: 5-10 clocks if compiler guess "which branch happen" wrong (this known "pipeline stall"); otherwise - 1 clock.

using information, might able make guesstimates ;-).

for other (non-x86) desktop/server/mobile cpus numbers different, overall picture more or less same.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -