English 中文(简体)
Simple read vs write boolean variable performance comparison question
原标题:

What should be the preferred way by programmers:

1) Only Write:

SomeBoolean = True

2) Read but write only if necessary

If Not SomeBoolean Then SomeBoolean = True
最佳回答

It s really hard to know the answer to this without knowing more about the environment. It seems a reasonable check would be to run some performance tests by iterating over this task many, many times.

Empirical evidence sometimes is surprising compared to what you d expect.

问题回答

Assuming you are referring to a RUNTIME context and a shared variable:

In a multiprocessor environment, unnecessary writes can lead to performance degradation: cache flush, synchronization overhead etc.

So YES it can make a difference... get profiling if the situation lends itself to it.

1) will be maybe a few nanoseconds faster. I suspect that compared to other things going on in your code, that difference is nanoscopic.

On the other hand, I usually write (2) if I might want to do something else when I know that I m actually changing the boolean. That gives me a place to do it.

The difference is negligible. You will see performance differences when reading and writing files to the drive, but program performance is measured in big O. Read:

BIG O notation

Especially in VB you will not see a difference.





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签