What uses less cycles? Writing a variable or checking it?

So if I have a large collection I’m cycling over and I want to reduce my cycle usage there are likely things I want to avoid. In Ethereum, writing a variable uses a bunch of gas so you try to avoid it. I don’t know what motoko does. Example:

if(thisItem.currentValue != newValue){ //changes are rare
      thisItem.currentValue := newValue;
};

or

thisItem.currentValue := newValue;

Which likely uses more cycles? If writing uses the same number of cycles as comparing (say these are Nats) then I’ll just write it every time and forget the comparison. But maybe the comparison is expensive?

3 Likes

If suspect that if you compile these both to Wasm and one file is bigger (contains more instructions) then I think you have your answer.

I’d say in these cases and our current GC stategy, writing is usually as cheap as reading.

Yeah, you don’t have to jump through hoops like that on the IC. That would fail to qualify as a general-purpose programming model.