x******a 发帖数: 6336 | 1 I need a class with some thing like
class foo{
std::string country; //an attribute
std::vector somefactors; //really need for calculation
...
}
The data members currency and somefactors are uniquely determined by the
country with a fooparameters class contains the map from country to the rest.
The reason I don't want to implement as a method like get_factors() is that
in reality I need to run large number of Monte-Carlo simulation
using somefactors and I really need the speed.
currently I initialize the class by order: first initialize country and
then somefactors by calling the fooparameters,
can you help me improve the class/pattern design?
thanks! |
D***n 发帖数: 6804 | 2 真复杂,如果你真想提高速度,请把这些乱七八糟的东西去掉。
int country; /* 整数国家代码 */
double ** somefactors; /* double 指针指向你的somefactor lookup table */
访问的时候
double * factorsByCountry = somefactors[country];
这多简单?
Country Name和Country整数的对应关系,搞一个数组比如countryName,里面成员是国
家字符串的首地址。 用的时候直接countryName[country]访问不就得了?
【在 x******a 的大作中提到】 : I need a class with some thing like : class foo{ : std::string country; //an attribute : std::vector somefactors; //really need for calculation : ... : } : The data members currency and somefactors are uniquely determined by the : country with a fooparameters class contains the map from country to the rest. : The reason I don't want to implement as a method like get_factors() is that : in reality I need to run large number of Monte-Carlo simulation
|
m********t 发帖数: 13072 | 3 using namespace std;
【在 D***n 的大作中提到】 : 真复杂,如果你真想提高速度,请把这些乱七八糟的东西去掉。 : int country; /* 整数国家代码 */ : double ** somefactors; /* double 指针指向你的somefactor lookup table */ : 访问的时候 : double * factorsByCountry = somefactors[country]; : 这多简单? : Country Name和Country整数的对应关系,搞一个数组比如countryName,里面成员是国 : 家字符串的首地址。 用的时候直接countryName[country]访问不就得了?
|
k**********g 发帖数: 989 | 4
rest.
that
Use C++11
Use a C++11 compiler that is less buggy than the other competitors
Use unordered_map, or "Boost multiindex"
For read-only access to the vector, write the read-only accessor as follows
const std::vector& getFactors() const { return m_someFactors; }
If you need to modify the vector, **as long as you don't change the vector
size and don't write to a location beyond its current size**, you can
actually just return a reference to the vector (without the const)
A C++ STL vector is guaranteed contiguous in memory. Therefore, you can take
the start pointer of a vector (the address of the first element of the
vector), and then use pointer-based access directly. This technique can be
useful if you know how to write C code **correctly**. Alternatively you can
also write SIMD code with this technique.
However, if your code is multi-threaded, you need to be extra careful, as
people always say. So my advice above should not be used in the multi-
threaded case.
【在 x******a 的大作中提到】 : I need a class with some thing like : class foo{ : std::string country; //an attribute : std::vector somefactors; //really need for calculation : ... : } : The data members currency and somefactors are uniquely determined by the : country with a fooparameters class contains the map from country to the rest. : The reason I don't want to implement as a method like get_factors() is that : in reality I need to run large number of Monte-Carlo simulation
|
k**********g 发帖数: 989 | 5 Also ask your coworkers to see if Eigen or NT2 (Numerical Template Toolbox)
can be used for your project. |
x******a 发帖数: 6336 | 6 Decon: Thanks for the answer, the problem is that there are a lot other
attributes besides the currency.
kinchungwong: thanks for your answer. I used to use pandas DataFrame
multiindex for this. However, the speed is slow. I will try boost multiindex. |
j******o 发帖数: 4219 | 7 如果你唯一的concern是速度的话,把所有country的属性都定义好,然后直接读取某个
country就行了,因为country的的数量和属性是固定的。 |