c++ - Setting or consulting boolean. Which has the best performance? -


just curiosity, process fastest: setting value boolean (ex: changing true false) or simple checking value (ex: if(boolean)...)

the problem have "which fastest" underspecified answered conclusively, , @ same time broad yield useful conclusions if answered conclusively. productive avenue curiosity can take build mental model of machine , run both cases through it.

foo = true stores value true location allocated foo. raises question: , how foo stored? impossible answer without running complete source code through compiler right settings. anywhere in ram, or in register, or use no storage @ all, being eliminated compiler optimizations. depending on foo resides, cost of overwriting can vary: hundreds of cpu cycles (if in ram , not in cache), couple cycles (in ram , cache), 1 cycle (register), 0 cycles (not stored).

if (foo) means reading foo , performing conditional branch based on it. regarding aspects i'll discuss here (i have omit many details , major categories), reading writing. conditional branch follows has less predictable, cost depends on run-time behavior of program. if branch taken, branch prediction make virtually free (a few cycles). if it's unpredictable, may take tens of cycles , blow pipeline (further reducing throughput). however, it's possible conditional code predicated, invaliding of above concerns , replacing reasoning instruction latency, data dependencies, , gory details of pipeline.

as can see sheer volume written (despite omitting many details , important secondary effects), it's virtually impossible answer in generality. need @ concrete, complete programs make sort of prediction, , need know whole system top bottom. note had assume specific kind of machine far: gpgpu or embedded system or 90's consumer cpu, i'd have rewrite of that.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -