views:

120

answers:

4

I have a class called "simulation" and a method for solving simulation.

class sim
{
    void Step()
    {
    }

    other methods (20+)...
}

Sim class is only instantiated once during the program.

Step method is called in the order of millions during the program.

Step method uses a lot of local variables (100+). None of those locals are used in other methods.

Is it better to make those local variables a member of the class or keep them as local in Step() for better performance?

+2  A: 

As a general rule, you should minimise the scope of variables and only increase the scope if it proves absolutely necessary. Converting locals to member variables is a poor design choice, and thus needs a very strong justification.

Also note that local variables only have a cost if they have non-trivial constructors. A local variable with a noop constructor or no constructor at all has no setup cost, so it would be pointless to expand its scope.

Marcelo Cantos
I use doubles but still creating them every call to func costs a lot.Performance increase is very promissing with converting locals to member variables in my tests. But the code is very ugly and hard to read in this current state :(
Kayhano
@Kayhano: Could you please explain how creating stack variables "costs a lot?"
bowenl2
@Kayhano: You weren't very clear on how you use the variables. I note in your comment to @Pontus Gagge that you store and reuse computed values across multiple calls. This isn't a stack-vs-member issue, then; it's a caching issue. Of course it will be faster if you precompute certain values and cache them across calls, which naturally requires the use of non-local variables, but this has nothing to do with the cost of "creating" stack variables, which is zero for doubles.
Marcelo Cantos
Consider this situation.class sim {void Step(){ double, x, y, z, ..... // other local variables}}and class sim{ double x, y, z, ..... void step() { // use x, y, z and others }}is there any performance difference between two? or creating local variables costs nothing?
Kayhano
+4  A: 

It depends. What kinds of variables: primitive types or objects? If the latter, your IL code is still going to chase their pointers. If the former, it depends on what order you access them in, and what CPU you are targeting. Optimizing the layout of variables should come pretty far down the list of performance tuning activities, especially in C# when you're dependent on what assembly your IL is translated into.

As usual with optimizations: first measure performance to identify bottlenecks. Then consider what you can do to remove them. Until you know that you need to do something like that, just write your code as clearly as possible: don't expose local variables unnecessarily by lifting them into the class level. And do consider splitting that Step() method: a large method is hard to understand and therefore even harder to optimize.

Pontus Gagge
All of the local variables are doubles.
Kayhano
i use local variables to increase speed. Most of them are storing equation values that are used several times.for example,// Calculate x axis distance.double dx = xp[i] - xp[j];and then i use dx in several more places.
Kayhano
A: 

If some of the variables contain objects that can be reused in between multiple method calls, you'd skip the overhead of disposing the old instance and creating a new instance with each method call. For example, lookups in a dependency injection container that won't change in between method calls could be 'cached' in a field.

But aside from that, you probably won't get much of a performance increase. You could give it a try and use a profiler to measure your code performance. A profiler may also help you to identify other bottlenecks in your code.

Niels van der Rest
A: 

Given the nature of your simulation code and the fact that the sim class is only instantiated once, have you considered the Singleton?

sc_ray
Why? This would just reduce performance by introducing unnecessary locking.
bowenl2
What is the scenario where it will cause unnecessary locking?
sc_ray