Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

performance - Which is faster? Comparison or assignment?

I'm doing a bit of coding, where I have to write this sort of code:

if( array[i]==false )
    array[i]=true;

I wonder if it should be re-written as

array[i]=true;

This raises the question: are comparisions faster than assignments?

What about differences from language to language? (contrast between java & cpp, eg.)

NOTE: I've heard that "premature optimization is the root of all evil." I don't think that applies here :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This isn't just premature optimization, this is micro-optimization, which is an irrelevant distraction.

Assuming your array is of boolean type then your comparison is unnecessary, which is the only relevant observation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...