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

Categories

In terms of memory and time, is it better to make a method static?

See Question&Answers more detail:os

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

1 Answer

Usually yes, there is no need to pass the "this" reference. That reference is passed in the ECX register so there is no additional stack space required. The register is already set if you make the call from an instance method of the same class, there will be no savings at all. But it can help relieving the pressure on a x86 CPU core when the method is in another class, x86 doesn't have a lot of registers. Seeing a measurable perf improvement would be exceedingly rare.

I do religiously mark methods of a class that don't use instance members as static. I value the inherent contract provided by the static keyword: "this method does not mutate the object state."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...