小编Fel*_*zen的帖子

为什么这种"优化"会减慢我的程序?

我正在编写一个图形引擎作为大学的任务,并且最近尝试优化我的部分代码,但是优化似乎会减慢它的速度.

代码的这一特定部分处理2D Lindenmayer系统并将它们转换为"line2D"对象列表,这些对象可以由程序的另一部分处理成图像.

在这样做时,它使用sin和cos来计算下一个点的坐标,并且因为sin和cos是浮点运算,我认为这些将是时间密集的,尤其是在更复杂的lindenmayer系统中.所以我创建了一个对象类"cossinlist",它从.txt文件中导入cos和sin的值,用于0到359度之间的每个整数角度(转换为rad)到两个名为"coslist"和"sinlist"的地图作为关键.这样我在处理包含小数部分的角度时只需要执行实际的触发器.

然后我决定用一个相对密集的系统来测量执行时间和优化(没有它)(通过评论它):使用它,引擎在33.4016秒内生成图像,没有它只需要25.3686秒.这是一个实质性的差异,但不是预期的方式.我做了更多的测试,他们都给出了相似的差异比例,所以现在我想知道......是什么导致了这种差异?

功能:

img::EasyImage LSystem2D(const unsigned int size, const ini::DoubleTuple & backgroundcolor, LParser::LSystem2D & System, const ini::DoubleTuple & color)
{
    CosSinList cossinlist;
    std::string string;
    Lines2D Lines;
    double origin = 0;
    Point2D currentpos(origin, origin);
    Point2D newpos(origin, origin);
    std::stack<Point2D> savedpositions;
    double currentangle = System.get_starting_angle();
    std::stack<double> savedangles;
    const img::Color linecolor(color.at(0)*255,color.at(1)*255,color.at(2)*255);
    const img::Color BGcolor(backgroundcolor.at(0)*255,backgroundcolor.at(1)*255,backgroundcolor.at(2)*255);
    string = ReplaceLsystem(System, (System.get_initiator()), (System.get_nr_iterations()));
    bool optimizedangle = false;
    if(System.get_angle() == rint(System.get_angle()) && (System.get_starting_angle() == rint(System.get_starting_angle()))
    {
        optimizedangle = true;
    }
    for(char& c : string)
    {
        if(currentangle …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point optimization

0
推荐指数
1
解决办法
239
查看次数

标签 统计

c++ ×1

floating-point ×1

optimization ×1