C
Chad Zalkin
We are evaluating some old code that was written as part of our math
library.
This code uses some optimizations that I'm not sure are necessary or
safe, but is a source of debate between my coworkers.
Method 1 includes a temporary storage varriable at class scope.
Method 2 includes a temporary storage varriable at method scope.
Method 3 includes a temporary static storage varriable at method scope.
Are any of the methods better than the others in terms of speed and
thread safety?
I don't believe method 1 or 3 are thread safe, but I think method 2 may
be too slow for a high-performance library. Is there a method 4 that
we should evaluate?
As an aside, I can't find in the standard where static is addressed in
terms of a member function. Is it true that a static varriable within
a member function exists for all instances?
/// ---- method 1
class vector {
....
private:
vector tempVector;
};
vector::somefunction() {
// writes to tempVector as temporary storage
}
/// ---- method 2
class vector {
....
};
vector::somefunction() {
vector tempVector;
// writes to tempVector as temporary storage
}
/// --- method 3
class vector {
....
};
vector::somefunction() {
static vector tempVector;
// writes to tempVector as temporary storage
}
library.
This code uses some optimizations that I'm not sure are necessary or
safe, but is a source of debate between my coworkers.
Method 1 includes a temporary storage varriable at class scope.
Method 2 includes a temporary storage varriable at method scope.
Method 3 includes a temporary static storage varriable at method scope.
Are any of the methods better than the others in terms of speed and
thread safety?
I don't believe method 1 or 3 are thread safe, but I think method 2 may
be too slow for a high-performance library. Is there a method 4 that
we should evaluate?
As an aside, I can't find in the standard where static is addressed in
terms of a member function. Is it true that a static varriable within
a member function exists for all instances?
/// ---- method 1
class vector {
....
private:
vector tempVector;
};
vector::somefunction() {
// writes to tempVector as temporary storage
}
/// ---- method 2
class vector {
....
};
vector::somefunction() {
vector tempVector;
// writes to tempVector as temporary storage
}
/// --- method 3
class vector {
....
};
vector::somefunction() {
static vector tempVector;
// writes to tempVector as temporary storage
}