Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libcstl效率问题 #11

Open
activesys opened this issue Feb 20, 2012 · 9 comments
Open

libcstl效率问题 #11

activesys opened this issue Feb 20, 2012 · 9 comments

Comments

@activesys
Copy link
Owner

[wb@ActiveSys libcstl]$ cat test_cpp_stl.cpp

void test_cpp_stl(void)
{
    std::vector<int> v1, v2;

    v1.assign(300, -39);
    v2 = v1;
}

int main(int argc, char* argv[])
{
    for (int i = 0; i < 100000; ++i) {
        test_cpp_stl();
    }
    return 0;
}

[wb@ActiveSys libcstl]$ cat test_cstl.c

void test_cstl(void)
{
    vector_t* pvec1 = NULL;
    vector_t* pvec2 = NULL;

    if ((pvec1 = create_vector(int)) == NULL) {
         return;
    }
    if ((pvec2 = create_vector(int)) == NULL) {
        vector_destroy(pvec1);
        return;
    }

    vector_init_elem(pvec1, 300, -39);
    vector_init(pvec2);
    vector_assign(pvec2, pvec1);

    vector_destroy(pvec1);
    vector_destroy(pvec2);
}

int main(int argc, char* argv[])
{
    int i = 0;
    for (i = 0; i < 100000; ++i) {
        test_cstl();
    }
    return 0;
}

[wb@ActiveSys libcstl]$ time ./test_cpp_stl

real 0m0.342s
user 0m0.303s
sys 0m0.021s
[wb@ActiveSys libcstl]$ time ./test_cstl

real 0m44.984s
user 0m42.134s
sys 0m0.039s

@Endle
Copy link

Endle commented May 4, 2013

你在 void test_cpp_stl 里手动调用两个 vector 的析构函数试试?

@activesys
Copy link
Owner Author

还是差不多的。

@suxiaojack
Copy link

确实性能差了很多啊~~vector在栈上,自动释放的,手动调用删除会程序崩溃的。
./configure
make

要42s左右

./configure --disable-assert
make
-O2
之后的也要10s左右
-O2 -static
要7s左右

@ghost
Copy link

ghost commented Sep 3, 2013

想请问一下,libcstl和C++的STL之间的效率差异是由于什么造成的?

@activesys
Copy link
Owner Author

stl的容器元素类型是静态的,在编译的时候就已经确定了,但是libcstl确实动态的,在运行的时候动态确定,我认为这是主要的问题,还有可能就是作者水平有限,选择的结构或者算法不好。

@activesys
Copy link
Owner Author

优化了类型解析部分的代码,性能提高了一倍,不过还是要比stl效率差。

@activesys
Copy link
Owner Author

libcstl-2.3以及后续版本要进行优化

@Kiddinglife
Copy link

Kiddinglife commented Feb 18, 2018

runtime = 0.022 ms cpp
runtime = 9.123 ms cstl
So libcstl is 500 times slower than stl. really?

@XuYan-sgpm
Copy link

我不太懂为什么libcstl要加上一个动态类型分析,原本C语言就无法实现想C++那样的完整的泛型机制,增加的这个动态类型识别大大的提高了程序的负担,而且还显得有些鸡肋,按照这样的一个运行效率,反而可用性不高了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants