[shlug] 求助:c程序的效率比java还慢

85 views
Skip to first unread message

Raymond Lau

unread,
Nov 20, 2012, 6:00:00 AM11/20/12
to sh...@googlegroups.com
---����
����������������� hacking �����۵ġ�ֻΪ��2�����Ķ���æ��û�п�ȥ�����Ծ���slug������һ���ˡ�
�������������ģ���ij�����ĵ�������Shell����ʱ�������performance��Ŀ(https://gitcafe.com/shell909090/performance)ͦ����Ȥ�ġ�
����fork��һ����Ȼ��ܿ���java ʵ����fibonacci��

��Ȼ�����ҵĻ����ϣ��������Ľ����Shell�����Ľ�����ܴ��ر���C ��


make c
../perf -l c ./fib-c
lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB

Shell��C�Ľ���ǣ�
+---------+----------+---------+---------+--------+
| lang | filesize | runtime | memory | remark |
+---------+----------+---------+---------+--------+
| c | 6.0 | 3.773 | 496.0 | |

������java��ȫģʽ����һ���ࣺ

make java-server
javac fib.java
../perf -l java-server java -server fib
lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB

������go��Ч�ʲ�ࣺ
make go
../perf -l go ./fib-go
lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB

Shell����go�Ľ���ǣ�
| go | 1242.0 | 7.441 | 880.0 | |

---���⣺

�ðɣ�������Щ������������:
��֪����Ӧ����gcc�����⣬����ȴ��֪��������gcc���������⣿��ľ����C�Ĵ�������

ps:
fib.c�ı������
gcc -O2 -o

gcc �汾��
gcc -v
ʹ���ڽ� specs��
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
Ŀ�꣺i686-pc-linux-gnu
������/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
�߳�ģ�ͣ�posix
gcc �汾 4.7.1 20120721 (prerelease) (GCC)

java�汾��
java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
OpenJDK Server VM (build 20.0-b12, mixed mode)

kernel�汾��
uname -a
Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux

ȫ���õ�archlinux����ġ�

Chaos Eternal

unread,
Nov 19, 2012, 10:06:57 PM11/19/12
to sh...@googlegroups.com
java代码拿来。


On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:
> ---背景
> 本来这个话题是想在 hacking 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的。
> 所以fork了一个,然后很快用java 实现了fibonacci。
>
> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :


>
>
> make c
> ../perf -l c ./fib-c
> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
>

> Shell的C的结果是:


> +---------+----------+---------+---------+--------+
> | lang | filesize | runtime | memory | remark |
> +---------+----------+---------+---------+--------+
> | c | 6.0 | 3.773 | 496.0 | |
>

> 甚至比java混全模式慢上一倍多:


>
> make java-server
> javac fib.java
> ../perf -l java-server java -server fib
> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
>

> 甚至与go的效率差不多:


> make go
> ../perf -l go ./fib-go
> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
>

> Shell公布go的结果是:
> | go | 1242.0 | 7.441 | 880.0 | |
>
> ---问题:
>
> 好吧,背景有些长,问题来了:
> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?
>
> ps:
> fib.c的编译参数
> gcc -O2 -o
>
> gcc 版本:
> gcc -v
> 使用内建 specs。
> COLLECT_GCC=gcc
> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
> 目标:i686-pc-linux-gnu
> 配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
> 线程模型:posix
> gcc 版本 4.7.1 20120721 (prerelease) (GCC)
>
> java版本:


> java -version
> java version "1.6.0_24"
> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
> OpenJDK Server VM (build 20.0-b12, mixed mode)
>

> kernel版本:


> uname -a
> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
>

> 全是用的archlinux库里的。
>
> --
> -- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
>
>

Raymond Lau

unread,
Nov 20, 2012, 6:35:28 AM11/20/12
to sh...@googlegroups.com
java ����:
public class fib {

static public long do_fib(final int n) {
if (n < 3)
return 1;
return do_fib(n - 1) + do_fib(n - 2);
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(do_fib(45));
}

}
����Ϊ������java��������⡣
C�����runtimeʱ��Ҳ̫����,12s ..̫������Щ����Shell��3.773 s���̫Զ��

������parse��������c��ʵ����ԭ���grep.. �����������ʱ���ҵĽ����Shell�Ľ����ز��󣬱���java,go��ȷʵ��Ҫ�߳�һ��ء�

�����һ��ǻ�������gcc ��������⡣��


On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
--Date: Tue, 20 Nov 2012 11:06:57 +0800
--From: Chaos Eternal <chaose...@shlug.org>
--To: sh...@googlegroups.com
--Subject: Re: [shlug] ����c�����Ч�ʱ�java����
--
--java����������
--
--
--On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:
--> ---����
--> ����������������� hacking �����۵ġ�ֻΪ��2�����Ķ���æ��û�п�ȥ�����Ծ���slug������һ���ˡ�
--> �������������ģ���ij�����ĵ�������Shell����ʱ�������performance��Ŀ(https://gitcafe.com/shell909090/performance)ͦ����Ȥ�ġ�
--> ����fork��һ����Ȼ��ܿ���java ʵ����fibonacci��
-->
--> ��Ȼ�����ҵĻ����ϣ��������Ľ����Shell�����Ľ�����ܴ��ر���C ��
-->
-->
--> make c
--> ../perf -l c ./fib-c
--> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
-->
--> Shell��C�Ľ���ǣ�
--> +---------+----------+---------+---------+--------+
--> | lang | filesize | runtime | memory | remark |
--> +---------+----------+---------+---------+--------+
--> | c | 6.0 | 3.773 | 496.0 | |
-->
--> ������java��ȫģʽ����һ���ࣺ
-->
--> make java-server
--> javac fib.java
--> ../perf -l java-server java -server fib
--> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
-->
--> ������go��Ч�ʲ�ࣺ
--> make go
--> ../perf -l go ./fib-go
--> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
-->
--> Shell����go�Ľ���ǣ�
--> | go | 1242.0 | 7.441 | 880.0 | |
-->
--> ---���⣺
-->
--> �ðɣ�������Щ������������:
--> ��֪����Ӧ����gcc�����⣬����ȴ��֪��������gcc���������⣿��ľ����C�Ĵ�������
-->
--> ps:
--> fib.c�ı������
--> gcc -O2 -o
-->
--> gcc �汾��
--> gcc -v
--> ʹ���ڽ� specs��
--> COLLECT_GCC=gcc
--> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
--> Ŀ�꣺i686-pc-linux-gnu
--> ������/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
--> �߳�ģ�ͣ�posix
--> gcc �汾 4.7.1 20120721 (prerelease) (GCC)
-->
--> java�汾��
--> java -version
--> java version "1.6.0_24"
--> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
--> OpenJDK Server VM (build 20.0-b12, mixed mode)
-->
--> kernel�汾��
--> uname -a
--> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
-->
--> ȫ���õ�archlinux����ġ�
-->
--> --
--> -- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
-->
-->
--
----
---- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
--
--

--
Raymond Lau

Liu Cheng

unread,
Nov 19, 2012, 10:38:19 PM11/19/12
to sh...@googlegroups.com
grep也不是c快,而是用了很多优化的技巧,和普通的用一下正则还是不同的

2012/11/20 Raymond Lau <ray...@uniteweb.net>:
> java 代码:


> public class fib {
>
> static public long do_fib(final int n) {
> if (n < 3)
> return 1;
> return do_fib(n - 1) + do_fib(n - 2);
> }
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> System.out.println(do_fib(45));
> }
>
> }

> 窃以为,不是java代码的问题。
> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。
>
> 另外在parse这个问题里,c的实现是原生的grep.. 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
>
> 所以我还是怀疑是我gcc 编译的问题。。


>
>
> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
> --Date: Tue, 20 Nov 2012 11:06:57 +0800
> --From: Chaos Eternal <chaose...@shlug.org>
> --To: sh...@googlegroups.com

> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --
> --java代码拿来。


> --
> --
> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:

> --> ---背景
> --> 本来这个话题是想在 hacking 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
> --> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的。
> --> 所以fork了一个,然后很快用java 实现了fibonacci。
> -->
> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :


> -->
> -->
> --> make c
> --> ../perf -l c ./fib-c
> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
> -->

> --> Shell的C的结果是:


> --> +---------+----------+---------+---------+--------+
> --> | lang | filesize | runtime | memory | remark |
> --> +---------+----------+---------+---------+--------+
> --> | c | 6.0 | 3.773 | 496.0 | |
> -->

> --> 甚至比java混全模式慢上一倍多:


> -->
> --> make java-server
> --> javac fib.java
> --> ../perf -l java-server java -server fib
> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
> -->

> --> 甚至与go的效率差不多:


> --> make go
> --> ../perf -l go ./fib-go
> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
> -->

> --> Shell公布go的结果是:


> --> | go | 1242.0 | 7.441 | 880.0 | |
> -->

> --> ---问题:
> -->
> --> 好吧,背景有些长,问题来了:
> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?
> -->
> --> ps:
> --> fib.c的编译参数


> --> gcc -O2 -o
> -->

> --> gcc 版本:
> --> gcc -v
> --> 使用内建 specs。
> --> COLLECT_GCC=gcc
> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
> --> 目标:i686-pc-linux-gnu
> --> 配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
> --> 线程模型:posix
> --> gcc 版本 4.7.1 20120721 (prerelease) (GCC)
> -->
> --> java版本:


> --> java -version
> --> java version "1.6.0_24"
> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
> -->

> --> kernel版本:


> --> uname -a
> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
> -->

> --> 全是用的archlinux库里的。

Chaos Eternal

unread,
Nov 19, 2012, 10:42:13 PM11/19/12
to sh...@googlegroups.com
那个程序我跑只要3秒,为了消除硬件、环境的差异,所以需要你的代码。最好还有makefile。

2012/11/20 Raymond Lau <ray...@uniteweb.net>:
> java 代码:

> public class fib {
>
> static public long do_fib(final int n) {
> if (n < 3)
> return 1;
> return do_fib(n - 1) + do_fib(n - 2);
> }
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> System.out.println(do_fib(45));
> }
>
> }

> 窃以为,不是java代码的问题。
> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。
>
> 另外在parse这个问题里,c的实现是原生的grep.. 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
>
> 所以我还是怀疑是我gcc 编译的问题。。
>
>

> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
> --Date: Tue, 20 Nov 2012 11:06:57 +0800
> --From: Chaos Eternal <chaose...@shlug.org>
> --To: sh...@googlegroups.com

> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --
> --java代码拿来。


> --
> --
> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:

> --> ---背景
> --> 本来这个话题是想在 hacking 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
> --> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的。
> --> 所以fork了一个,然后很快用java 实现了fibonacci。
> -->
> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :

> -->
> -->
> --> make c
> --> ../perf -l c ./fib-c
> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
> -->

> --> Shell的C的结果是:


> --> +---------+----------+---------+---------+--------+
> --> | lang | filesize | runtime | memory | remark |
> --> +---------+----------+---------+---------+--------+
> --> | c | 6.0 | 3.773 | 496.0 | |
> -->

> --> 甚至比java混全模式慢上一倍多:


> -->
> --> make java-server
> --> javac fib.java
> --> ../perf -l java-server java -server fib
> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
> -->

> --> 甚至与go的效率差不多:


> --> make go
> --> ../perf -l go ./fib-go
> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
> -->

> --> Shell公布go的结果是:


> --> | go | 1242.0 | 7.441 | 880.0 | |
> -->

> --> ---问题:
> -->
> --> 好吧,背景有些长,问题来了:
> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?
> -->
> --> ps:
> --> fib.c的编译参数

> --> gcc -O2 -o
> -->

> --> gcc 版本:
> --> gcc -v

> --> 使用内建 specs。
> --> COLLECT_GCC=gcc
> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper


> --> 目标:i686-pc-linux-gnu
> --> 配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
> --> 线程模型:posix

> --> gcc 版本 4.7.1 20120721 (prerelease) (GCC)
> -->
> --> java版本:


> --> java -version
> --> java version "1.6.0_24"
> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
> -->

> --> kernel版本:


> --> uname -a
> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
> -->

> --> 全是用的archlinux库里的。

Chaos Eternal

unread,
Nov 19, 2012, 10:51:30 PM11/19/12
to sh...@googlegroups.com
拿你的代码我测了一下,
java跑3.60
C跑3.70

相差不大。

看来确实是你的环境问题。


2012/11/20 Chaos Eternal <chaose...@shlug.org>:

Raymond Lau

unread,
Nov 20, 2012, 7:40:03 AM11/20/12
to sh...@googlegroups.com

�������һֱ������22�˿ڣ�һֱû��push��gitcafe��ȥ��
�������Ҹ��ط���push��ȥ��

��Ҳ֪��������gcc�����������ˡ���Щʱ�Ź���Σ�һֱû��ͷ����

���Կ���slug����ľ��C����Ĵ�ţ ���ܰ�æ����¿�����

PS:¥�ϻ����ϸ����ˡ�java��3.6s.. ˵ʵ����java ���ܵ�5s�Ҷ��������ˡ���

On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
--Date: Tue, 20 Nov 2012 11:51:30 +0800
--From: Chaos Eternal <chaose...@shlug.org>
--To: sh...@googlegroups.com
--Subject: Re: [shlug] ����c�����Ч�ʱ�java����
--
--����Ĵ����Ҳ���һ�£�
--java��3.60
--C��3.70
--
--����
--
--����ȷʵ����Ļ������⡣
--
--
--2012/11/20 Chaos Eternal <chaose...@shlug.org>:
--> �Ǹ���������ֻҪ3�룬Ϊ�����Ӳ���������IJ��죬������Ҫ��Ĵ��롣��û���makefile��
-->
--> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:
-->> java ����:
-->> public class fib {
-->>
-->> static public long do_fib(final int n) {
-->> if (n < 3)
-->> return 1;
-->> return do_fib(n - 1) + do_fib(n - 2);
-->> }
-->>
-->> /**
-->> * @param args
-->> */
-->> public static void main(String[] args) {
-->> System.out.println(do_fib(45));
-->> }
-->>
-->> }
-->> ����Ϊ������java��������⡣
-->> C�����runtimeʱ��Ҳ̫����,12s ..̫������Щ����Shell��3.773 s���̫Զ��
-->>
-->> ������parse��������c��ʵ����ԭ���grep.. �����������ʱ���ҵĽ����Shell�Ľ����ز��󣬱���java,go��ȷʵ��Ҫ�߳�һ��ء�
-->>
-->> �����һ��ǻ�������gcc ��������⡣��
-->>
-->>
-->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
-->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
-->> --From: Chaos Eternal <chaose...@shlug.org>
-->> --To: sh...@googlegroups.com
-->> --Subject: Re: [shlug] ����c�����Ч�ʱ�java����
-->> --
-->> --java����������
-->> --
-->> --
-->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:
-->> --> ---����
-->> --> ����������������� hacking �����۵ġ�ֻΪ��2�����Ķ���æ��û�п�ȥ�����Ծ���slug������һ���ˡ�
-->> --> �������������ģ���ij�����ĵ�������Shell����ʱ�������performance��Ŀ(https://gitcafe.com/shell909090/performance)ͦ����Ȥ�ġ�
-->> --> ����fork��һ����Ȼ��ܿ���java ʵ����fibonacci��
-->> -->
-->> --> ��Ȼ�����ҵĻ����ϣ��������Ľ����Shell�����Ľ�����ܴ��ر���C ��
-->> -->
-->> -->
-->> --> make c
-->> --> ../perf -l c ./fib-c
-->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
-->> -->
-->> --> Shell��C�Ľ���ǣ�
-->> --> +---------+----------+---------+---------+--------+
-->> --> | lang | filesize | runtime | memory | remark |
-->> --> +---------+----------+---------+---------+--------+
-->> --> | c | 6.0 | 3.773 | 496.0 | |
-->> -->
-->> --> ������java��ȫģʽ����һ���ࣺ
-->> -->
-->> --> make java-server
-->> --> javac fib.java
-->> --> ../perf -l java-server java -server fib
-->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
-->> -->
-->> --> ������go��Ч�ʲ�ࣺ
-->> --> make go
-->> --> ../perf -l go ./fib-go
-->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
-->> -->
-->> --> Shell����go�Ľ���ǣ�
-->> --> | go | 1242.0 | 7.441 | 880.0 | |
-->> -->
-->> --> ---���⣺
-->> -->
-->> --> �ðɣ�������Щ������������:
-->> --> ��֪����Ӧ����gcc�����⣬����ȴ��֪��������gcc���������⣿��ľ����C�Ĵ�������
-->> -->
-->> --> ps:
-->> --> fib.c�ı������
-->> --> gcc -O2 -o
-->> -->
-->> --> gcc �汾��
-->> --> gcc -v
-->> --> ʹ���ڽ� specs��
-->> --> COLLECT_GCC=gcc
-->> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
-->> --> Ŀ�꣺i686-pc-linux-gnu
-->> --> ������/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
-->> --> �߳�ģ�ͣ�posix
-->> --> gcc �汾 4.7.1 20120721 (prerelease) (GCC)
-->> -->
-->> --> java�汾��
-->> --> java -version
-->> --> java version "1.6.0_24"
-->> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
-->> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
-->> -->
-->> --> kernel�汾��
-->> --> uname -a
-->> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
-->> -->
-->> --> ȫ���õ�archlinux����ġ�
-->> -->
-->> --> --
-->> --> -- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
-->> -->
-->> -->
-->> --
-->> ----
-->> ---- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
-->> --
-->> --
-->>
-->> --
-->> Raymond Lau

Chaos Eternal

unread,
Nov 19, 2012, 11:49:55 PM11/19/12
to sh...@googlegroups.com
放大招
gcc -s
然后上结果。


2012/11/20 Raymond Lau <ray...@uniteweb.net>:
>
> 最近网络一直都封了22端口,一直没能push到gitcafe上去。
> 晚上我找个地方先push上去。
>
> 我也知道多半就是gcc环境的问题了。这些时放狗过几次,一直没有头绪。
>
> 所以看看slug里有木有C方面的大牛 ,能帮忙解决下看看。
>
> PS:楼上机器老给力了。java跑3.6s.. 说实话,java 能跑到5s我都老满足了。。


>
> On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
> --Date: Tue, 20 Nov 2012 11:51:30 +0800
> --From: Chaos Eternal <chaose...@shlug.org>
> --To: sh...@googlegroups.com

> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --
> --拿你的代码我测了一下,
> --java跑3.60
> --C跑3.70
> --
> --相差不大。
> --
> --看来确实是你的环境问题。


> --
> --
> --2012/11/20 Chaos Eternal <chaose...@shlug.org>:

> --> 那个程序我跑只要3秒,为了消除硬件、环境的差异,所以需要你的代码。最好还有makefile。


> -->
> --> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:

> -->> java 代码:


> -->> public class fib {
> -->>
> -->> static public long do_fib(final int n) {
> -->> if (n < 3)
> -->> return 1;
> -->> return do_fib(n - 1) + do_fib(n - 2);
> -->> }
> -->>
> -->> /**
> -->> * @param args
> -->> */
> -->> public static void main(String[] args) {
> -->> System.out.println(do_fib(45));
> -->> }
> -->>
> -->> }

> -->> 窃以为,不是java代码的问题。
> -->> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。
> -->>
> -->> 另外在parse这个问题里,c的实现是原生的grep.. 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
> -->>
> -->> 所以我还是怀疑是我gcc 编译的问题。。


> -->>
> -->>
> -->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
> -->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
> -->> --From: Chaos Eternal <chaose...@shlug.org>
> -->> --To: sh...@googlegroups.com

> -->> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> -->> --
> -->> --java代码拿来。


> -->> --
> -->> --
> -->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:

> -->> --> ---背景
> -->> --> 本来这个话题是想在 hacking 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
> -->> --> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的。
> -->> --> 所以fork了一个,然后很快用java 实现了fibonacci。


> -->> -->
> -->> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :

> -->> -->
> -->> -->
> -->> --> make c
> -->> --> ../perf -l c ./fib-c
> -->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
> -->> -->

> -->> --> Shell的C的结果是:


> -->> --> +---------+----------+---------+---------+--------+
> -->> --> | lang | filesize | runtime | memory | remark |
> -->> --> +---------+----------+---------+---------+--------+
> -->> --> | c | 6.0 | 3.773 | 496.0 | |
> -->> -->

> -->> --> 甚至比java混全模式慢上一倍多:


> -->> -->
> -->> --> make java-server
> -->> --> javac fib.java
> -->> --> ../perf -l java-server java -server fib
> -->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
> -->> -->

> -->> --> 甚至与go的效率差不多:


> -->> --> make go
> -->> --> ../perf -l go ./fib-go
> -->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
> -->> -->

> -->> --> Shell公布go的结果是:


> -->> --> | go | 1242.0 | 7.441 | 880.0 | |
> -->> -->

> -->> --> ---问题:
> -->> -->


> -->> --> 好吧,背景有些长,问题来了:

> -->> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?


> -->> -->
> -->> --> ps:

> -->> --> fib.c的编译参数


> -->> --> gcc -O2 -o
> -->> -->

> -->> --> gcc 版本:
> -->> --> gcc -v
> -->> --> 使用内建 specs。


> -->> --> COLLECT_GCC=gcc
> -->> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper

> -->> --> 目标:i686-pc-linux-gnu
> -->> --> 配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
> -->> --> 线程模型:posix
> -->> --> gcc 版本 4.7.1 20120721 (prerelease) (GCC)
> -->> -->
> -->> --> java版本:


> -->> --> java -version
> -->> --> java version "1.6.0_24"
> -->> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
> -->> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
> -->> -->

> -->> --> kernel版本:


> -->> --> uname -a
> -->> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
> -->> -->

> -->> --> 全是用的archlinux库里的。

Raymond Lau

unread,
Nov 20, 2012, 8:05:28 AM11/20/12
to sh...@googlegroups.com

gcc -S �Ľ���ڸ���


On Tue, Nov 20, 2012 at 12:49:55PM +0800, Chaos Eternal wrote:
--Date: Tue, 20 Nov 2012 12:49:55 +0800
--From: Chaos Eternal <chaose...@shlug.org>
--To: sh...@googlegroups.com
--Subject: Re: [shlug] ����c�����Ч�ʱ�java����
--
--�Ŵ���
--gcc -s
--Ȼ���Ͻ��
--
--
--2012/11/20 Raymond Lau <ray...@uniteweb.net>:
-->
--> �������һֱ������22�˿ڣ�һֱû��push��gitcafe��ȥ��
--> �������Ҹ��ط���push��ȥ��
-->
--> ��Ҳ֪��������gcc�����������ˡ���Щʱ�Ź���Σ�һֱû��ͷ����
-->
--> ���Կ���slug����ľ��C����Ĵ�ţ ���ܰ�æ����¿�����
-->
--> PS:¥�ϻ����ϸ����ˡ�java��3.6s.. ˵ʵ����java ���ܵ�5s�Ҷ��������ˡ���
-->
--> On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
--> --Date: Tue, 20 Nov 2012 11:51:30 +0800
--> --From: Chaos Eternal <chaose...@shlug.org>
--> --To: sh...@googlegroups.com
--> --Subject: Re: [shlug] ����c�����Ч�ʱ�java����
--> --
--> --����Ĵ����Ҳ���һ�£�
--> --java��3.60
--> --C��3.70
--> --
--> --����
--> --
--> --����ȷʵ����Ļ������⡣
--> --
--> --
--> --2012/11/20 Chaos Eternal <chaose...@shlug.org>:
--> --> �Ǹ���������ֻҪ3�룬Ϊ�����Ӳ���������IJ��죬������Ҫ��Ĵ��롣��û���makefile��
--> -->
--> --> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:
--> -->> java ����:
--> -->> public class fib {
--> -->>
--> -->> static public long do_fib(final int n) {
--> -->> if (n < 3)
--> -->> return 1;
--> -->> return do_fib(n - 1) + do_fib(n - 2);
--> -->> }
--> -->>
--> -->> /**
--> -->> * @param args
--> -->> */
--> -->> public static void main(String[] args) {
--> -->> System.out.println(do_fib(45));
--> -->> }
--> -->>
--> -->> }
--> -->> ����Ϊ������java��������⡣
--> -->> C�����runtimeʱ��Ҳ̫����,12s ..̫������Щ����Shell��3.773 s���̫Զ��
--> -->>
--> -->> ������parse��������c��ʵ����ԭ���grep.. �����������ʱ���ҵĽ����Shell�Ľ����ز��󣬱���java,go��ȷʵ��Ҫ�߳�һ��ء�
--> -->>
--> -->> �����һ��ǻ�������gcc ��������⡣��
--> -->>
--> -->>
--> -->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
--> -->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
--> -->> --From: Chaos Eternal <chaose...@shlug.org>
--> -->> --To: sh...@googlegroups.com
--> -->> --Subject: Re: [shlug] ����c�����Ч�ʱ�java����
--> -->> --
--> -->> --java����������
--> -->> --
--> -->> --
--> -->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:
--> -->> --> ---����
--> -->> --> ����������������� hacking �����۵ġ�ֻΪ��2�����Ķ���æ��û�п�ȥ�����Ծ���slug������һ���ˡ�
--> -->> --> �������������ģ���ij�����ĵ�������Shell����ʱ�������performance��Ŀ(https://gitcafe.com/shell909090/performance)ͦ����Ȥ�ġ�
--> -->> --> ����fork��һ����Ȼ��ܿ���java ʵ����fibonacci��
--> -->> -->
--> -->> --> ��Ȼ�����ҵĻ����ϣ��������Ľ����Shell�����Ľ�����ܴ��ر���C ��
--> -->> -->
--> -->> -->
--> -->> --> make c
--> -->> --> ../perf -l c ./fib-c
--> -->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
--> -->> -->
--> -->> --> Shell��C�Ľ���ǣ�
--> -->> --> +---------+----------+---------+---------+--------+
--> -->> --> | lang | filesize | runtime | memory | remark |
--> -->> --> +---------+----------+---------+---------+--------+
--> -->> --> | c | 6.0 | 3.773 | 496.0 | |
--> -->> -->
--> -->> --> ������java��ȫģʽ����һ���ࣺ
--> -->> -->
--> -->> --> make java-server
--> -->> --> javac fib.java
--> -->> --> ../perf -l java-server java -server fib
--> -->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
--> -->> -->
--> -->> --> ������go��Ч�ʲ�ࣺ
--> -->> --> make go
--> -->> --> ../perf -l go ./fib-go
--> -->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
--> -->> -->
--> -->> --> Shell����go�Ľ���ǣ�
--> -->> --> | go | 1242.0 | 7.441 | 880.0 | |
--> -->> -->
--> -->> --> ---���⣺
--> -->> -->
--> -->> --> �ðɣ�������Щ������������:
--> -->> --> ��֪����Ӧ����gcc�����⣬����ȴ��֪��������gcc���������⣿��ľ����C�Ĵ�������
--> -->> -->
--> -->> --> ps:
--> -->> --> fib.c�ı������
--> -->> --> gcc -O2 -o
--> -->> -->
--> -->> --> gcc �汾��
--> -->> --> gcc -v
--> -->> --> ʹ���ڽ� specs��
--> -->> --> COLLECT_GCC=gcc
--> -->> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
--> -->> --> Ŀ�꣺i686-pc-linux-gnu
--> -->> --> ������/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
--> -->> --> �߳�ģ�ͣ�posix
--> -->> --> gcc �汾 4.7.1 20120721 (prerelease) (GCC)
--> -->> -->
--> -->> --> java�汾��
--> -->> --> java -version
--> -->> --> java version "1.6.0_24"
--> -->> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
--> -->> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
--> -->> -->
--> -->> --> kernel�汾��
--> -->> --> uname -a
--> -->> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
--> -->> -->
--> -->> --> ȫ���õ�archlinux����ġ�
--> -->> -->
--> -->> --> --
--> -->> --> -- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
--> -->> -->
--> -->> -->
--> -->> --
--> -->> ----
--> -->> ---- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
fib-c.s

Risingv

unread,
Nov 20, 2012, 12:22:32 AM11/20/12
to sh...@googlegroups.com
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

同样是C要慢一倍,何解?

另外也看到别人测试到java的递归效率高于C

在 2012年11月20日 下午9:05,Raymond Lau <ray...@uniteweb.net>写道:

gcc -S 的结果在附件



On Tue, Nov 20, 2012 at 12:49:55PM +0800, Chaos Eternal wrote:
--Date: Tue, 20 Nov 2012 12:49:55 +0800
--From: Chaos Eternal <chaose...@shlug.org>
--To: sh...@googlegroups.com
--Subject: Re: [shlug] 求助:c程序的效率比java还慢
--
--放大招
--gcc -s
--然后上结果。

--
--
--2012/11/20 Raymond Lau <ray...@uniteweb.net>:
-->
--> 最近网络一直都封了22端口,一直没能push到gitcafe上去。
--> 晚上我找个地方先push上去。
-->
--> 我也知道多半就是gcc环境的问题了。这些时放狗过几次,一直没有头绪。
-->
--> 所以看看slug里有木有C方面的大牛  ,能帮忙解决下看看。
-->
--> PS:楼上机器老给力了。java跑3.6s.. 说实话,java 能跑到5s我都老满足了。。

-->
--> On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
--> --Date: Tue, 20 Nov 2012 11:51:30 +0800
--> --From: Chaos Eternal <chaose...@shlug.org>
--> --To: sh...@googlegroups.com
--> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
--> --
--> --拿你的代码我测了一下,
--> --java跑3.60
--> --C跑3.70
--> --
--> --相差不大。
--> --
--> --看来确实是你的环境问题。

--> --
--> --
--> --2012/11/20 Chaos Eternal <chaose...@shlug.org>:
--> --> 那个程序我跑只要3秒,为了消除硬件、环境的差异,所以需要你的代码。最好还有makefile。

--> -->
--> --> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:
--> -->> java 代码:

--> -->> public class fib {
--> -->>
--> -->>         static public long do_fib(final int n) {
--> -->>                 if (n < 3)
--> -->>                         return 1;
--> -->>                 return do_fib(n - 1) + do_fib(n - 2);
--> -->>         }
--> -->>
--> -->>         /**
--> -->>          * @param args
--> -->>          */
--> -->>         public static void main(String[] args) {
--> -->>                 System.out.println(do_fib(45));
--> -->>         }
--> -->>
--> -->> }
--> -->> 窃以为,不是java代码的问题。
--> -->> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。
--> -->>
--> -->> 另外在parse这个问题里,c的实现是原生的grep.. 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
--> -->>
--> -->> 所以我还是怀疑是我gcc 编译的问题。。
--> -->>
--> -->>
--> -->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
--> -->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
--> -->> --From: Chaos Eternal <chaose...@shlug.org>
--> -->> --To: sh...@googlegroups.com
--> -->> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
--> -->> --
--> -->> --java代码拿来。
--> -->> --
--> -->> --
--> -->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:
--> -->> --> ---背景
--> -->> --> 本来这个话题是想在 hacking  上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
--> -->> --> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的
--> -->> --> 所以fork了一个,然后很快用java 实现了fibonacci。
--> -->> -->
--> -->> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :

--> -->> -->
--> -->> -->
--> -->> --> make  c
--> -->> --> ../perf -l c ./fib-c
--> -->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
--> -->> -->
--> -->> --> Shell的C的结果是:
--> -->> --> +---------+----------+---------+---------+--------+
--> -->> --> |   lang  | filesize | runtime |  memory | remark |
--> -->> --> +---------+----------+---------+---------+--------+
--> -->> --> |    c    |   6.0    |  3.773  |  496.0  |        |
--> -->> -->
--> -->> --> 甚至比java混全模式慢上一倍多:

--> -->> -->
--> -->> --> make   java-server
--> -->> --> javac   fib.java
--> -->> --> ../perf -l java-server java -server fib
--> -->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
--> -->> -->
--> -->> --> 甚至与go的效率差不多:
--> -->> -->  make go
--> -->> --> ../perf -l go ./fib-go
--> -->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
--> -->> -->
--> -->> --> Shell公布go的结果是:
--> -->> --> |    go   |  1242.0  |  7.441  |  880.0  |        |
--> -->> -->
--> -->> --> ---问题:
--> -->> -->

--> -->> --> 好吧,背景有些长,问题来了:
--> -->> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?

--> -->> -->
--> -->> --> ps:
--> -->> --> fib.c的编译参数

--> -->> -->     gcc -O2 -o
--> -->> -->
--> -->> --> gcc 版本:
--> -->> -->     gcc  -v
--> -->> -->     使用内建 specs。

--> -->> -->     COLLECT_GCC=gcc
--> -->> -->     COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
--> -->> -->     目标:i686-pc-linux-gnu
--> -->> -->     配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
--> -->> -->     线程模型:posix
--> -->> -->     gcc 版本 4.7.1 20120721 (prerelease) (GCC)
--> -->> -->
--> -->> --> java版本:
--> -->> -->     java -version
--> -->> -->     java version "1.6.0_24"
--> -->> -->     OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
--> -->> -->     OpenJDK Server VM (build 20.0-b12, mixed mode)
--> -->> -->
--> -->> --> kernel版本:
--> -->> -->      uname -a
--> -->> -->     Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
--> -->> -->
--> -->> --> 全是用的archlinux库里的。



--
Life is in chaos, keep struggling.
Personal blog: risingv.in

Risingv

unread,
Nov 20, 2012, 12:25:42 AM11/20/12
to sh...@googlegroups.com
难道jvm将java的递归转化成迭代来实现??这样能快点,就不足为奇了

Chaos Eternal

unread,
Nov 20, 2012, 12:29:24 AM11/20/12
to sh...@googlegroups.com
这个不可能。因为这个不是尾递归。


2012/11/20 Risingv <franc...@gmail.com>:

Risingv

unread,
Nov 20, 2012, 12:43:33 AM11/20/12
to sh...@googlegroups.com
嗯,我的表述不够准确,不是指递归直接转迭代
fibonacci数列本身具有大量重叠的子问题,
如果用动态规划之类的手段把前面的调用结果保存下来,需要的时候直接调用。
而这个工作由JVM完成,这样就能快一点了。。。
纯属猜测,没有依据佐证啊。

Chaos Eternal

unread,
Nov 20, 2012, 12:45:04 AM11/20/12
to sh...@googlegroups.com
openjdk可以看代码的。

2012/11/20 Risingv <franc...@gmail.com>:

Chaos Eternal

unread,
Nov 20, 2012, 12:54:40 AM11/20/12
to sh...@googlegroups.com
看了一下,除了是32位的,好像看不出啥。

另外,能不能让你的mutt在回复邮件的时候,引用字符改成 > 啊。。。

2012/11/20 Raymond Lau <ray...@uniteweb.net>:
>
> gcc -S 的结果在附件


>
>
> On Tue, Nov 20, 2012 at 12:49:55PM +0800, Chaos Eternal wrote:
> --Date: Tue, 20 Nov 2012 12:49:55 +0800
> --From: Chaos Eternal <chaose...@shlug.org>
> --To: sh...@googlegroups.com

> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --
> --放大招
> --gcc -s
> --然后上结果。

> --
> --
> --2012/11/20 Raymond Lau <ray...@uniteweb.net>:
> -->

> --> 最近网络一直都封了22端口,一直没能push到gitcafe上去。
> --> 晚上我找个地方先push上去。
> -->
> --> 我也知道多半就是gcc环境的问题了。这些时放狗过几次,一直没有头绪。
> -->
> --> 所以看看slug里有木有C方面的大牛 ,能帮忙解决下看看。
> -->

> --> PS:楼上机器老给力了。java跑3.6s.. 说实话,java 能跑到5s我都老满足了。。


> -->
> --> On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
> --> --Date: Tue, 20 Nov 2012 11:51:30 +0800
> --> --From: Chaos Eternal <chaose...@shlug.org>
> --> --To: sh...@googlegroups.com

> --> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --> --

> --> --拿你的代码我测了一下,
> --> --java跑3.60
> --> --C跑3.70
> --> --
> --> --相差不大。
> --> --
> --> --看来确实是你的环境问题。

> --> --
> --> --
> --> --2012/11/20 Chaos Eternal <chaose...@shlug.org>:

> --> --> 那个程序我跑只要3秒,为了消除硬件、环境的差异,所以需要你的代码。最好还有makefile。


> --> -->
> --> --> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:

> --> -->> java 代码:


> --> -->> public class fib {
> --> -->>
> --> -->> static public long do_fib(final int n) {
> --> -->> if (n < 3)
> --> -->> return 1;
> --> -->> return do_fib(n - 1) + do_fib(n - 2);
> --> -->> }
> --> -->>
> --> -->> /**
> --> -->> * @param args
> --> -->> */
> --> -->> public static void main(String[] args) {
> --> -->> System.out.println(do_fib(45));
> --> -->> }
> --> -->>
> --> -->> }

> --> -->> 窃以为,不是java代码的问题。
> --> -->> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。


> --> -->>
> --> -->> 另外在parse这个问题里,c的实现是原生的grep.. 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
> --> -->>

> --> -->> 所以我还是怀疑是我gcc 编译的问题。。


> --> -->>
> --> -->>
> --> -->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal wrote:
> --> -->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
> --> -->> --From: Chaos Eternal <chaose...@shlug.org>
> --> -->> --To: sh...@googlegroups.com

> --> -->> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
> --> -->> --
> --> -->> --java代码拿来。


> --> -->> --
> --> -->> --
> --> -->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau <ray...@uniteweb.net> wrote:

> --> -->> --> ---背景
> --> -->> --> 本来这个话题是想在 hacking 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
> --> -->> --> 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的。
> --> -->> --> 所以fork了一个,然后很快用java 实现了fibonacci。
> --> -->> -->
> --> -->> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :


> --> -->> -->
> --> -->> -->
> --> -->> --> make c
> --> -->> --> ../perf -l c ./fib-c
> --> -->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory: 392 kB
> --> -->> -->

> --> -->> --> Shell的C的结果是:


> --> -->> --> +---------+----------+---------+---------+--------+
> --> -->> --> | lang | filesize | runtime | memory | remark |
> --> -->> --> +---------+----------+---------+---------+--------+
> --> -->> --> | c | 6.0 | 3.773 | 496.0 | |
> --> -->> -->

> --> -->> --> 甚至比java混全模式慢上一倍多:


> --> -->> -->
> --> -->> --> make java-server
> --> -->> --> javac fib.java
> --> -->> --> ../perf -l java-server java -server fib
> --> -->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s, memory: 15376 kB
> --> -->> -->

> --> -->> --> 甚至与go的效率差不多:


> --> -->> --> make go
> --> -->> --> ../perf -l go ./fib-go
> --> -->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s, memory: 852 kB
> --> -->> -->

> --> -->> --> Shell公布go的结果是:


> --> -->> --> | go | 1242.0 | 7.441 | 880.0 | |
> --> -->> -->

> --> -->> --> ---问题:
> --> -->> -->


> --> -->> --> 好吧,背景有些长,问题来了:

> --> -->> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?


> --> -->> -->
> --> -->> --> ps:

> --> -->> --> fib.c的编译参数


> --> -->> --> gcc -O2 -o
> --> -->> -->

> --> -->> --> gcc 版本:


> --> -->> --> gcc -v

> --> -->> --> 使用内建 specs。


> --> -->> --> COLLECT_GCC=gcc
> --> -->> --> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper

> --> -->> --> 目标:i686-pc-linux-gnu

> --> -->> --> 配置为:/build/src/gcc-4.7-20120721/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release
> --> -->> --> 线程模型:posix
> --> -->> --> gcc 版本 4.7.1 20120721 (prerelease) (GCC)
> --> -->> -->
> --> -->> --> java版本:


> --> -->> --> java -version
> --> -->> --> java version "1.6.0_24"
> --> -->> --> OpenJDK Runtime Environment (IcedTea6 1.11.4) (ArchLinux-6.b24_1.11.4-1-i686)
> --> -->> --> OpenJDK Server VM (build 20.0-b12, mixed mode)
> --> -->> -->

> --> -->> --> kernel版本:


> --> -->> --> uname -a
> --> -->> --> Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun Sep 9 18:09:00 UTC 2012 i686 GNU/Linux
> --> -->> -->

> --> -->> --> 全是用的archlinux库里的。

SuperCat

unread,
Nov 20, 2012, 12:55:39 AM11/20/12
to sh...@googlegroups.com
superlynx@superlynx-server-develop:~/src$ time ./test5
1134903170

real    0m3.891s
user    0m3.852s
sys    0m0.004s
superlynx@superlynx-server-develop:~/src$ time java fib
1134903170

real    0m7.497s
user    0m7.364s
sys    0m0.024s

差距还是很大的

2012/11/20 Chaos Eternal <chaose...@shlug.org>

YiZhi Liu

unread,
Nov 20, 2012, 1:01:27 AM11/20/12
to shlug
这JVM也太高端了吧。JVM会自动优化经常reach到的代码倒是真的
Yizhi Liu
Software Engineer / Data Mining
Mediav.com, Shanghai, China

z7z8th

unread,
Nov 20, 2012, 2:25:49 AM11/20/12
to sh...@googlegroups.com
3.2.0-3-amd64 #1 SMP Mon Jul 23 02:45:17 UTC 2012 x86_64 GNU/Linux

$ javac fib.java
$ time java fib
1134903170
java fib  5.65s user 0.00s system 99% cpu 5.667 total

$ gcc -O2 fib.c -Wall
$ time ./a.out
./a.out  10.54s user 0.00s system 99% cpu 10.570 total

$ gcc -O3 fib.c -Wall
$ time ./a.out      
./a.out  3.33s user 0.00s system 99% cpu 3.340 total

-O3的话就比java快了,看gcc -S -O3出来的汇编比-O2的多,-O2的时候只是中规中的递归,-O3就看不懂啥意思了。。。

Xidorn Quan

unread,
Nov 20, 2012, 3:11:27 AM11/20/12
to sh...@googlegroups.com
我这里的结果是使用 clang,-O2 就可以达到 2s+ 了,如果 -O0 的话是 7s+。
研究了一下汇编,发现打开 -O2 的情况下,两次递归中的一次被消解为了迭代。


2012/11/20 z7z8th <ybzha...@gmail.com>

依云

unread,
Nov 20, 2012, 3:22:01 AM11/20/12
to sh...@googlegroups.com
>>> time java fib
1134903170
java fib 4.85s user 0.01s system 99% cpu 4.879 total
>>> time ./fib-c
1134903170
./fib-c 2.40s user 0.00s system 99% cpu 2.405 total

另外,同讨厌引用字符不规矩的 :-(
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

David pulq

unread,
Nov 20, 2012, 3:36:16 AM11/20/12
to sh...@googlegroups.com
说明选一个好的算法是多么的重要啊。
非递归的版本
real 0m0.001s
user 0m0.000s
sys 0m0.000s

Chaos Eternal

unread,
Nov 20, 2012, 3:37:29 AM11/20/12
to sh...@googlegroups.com
没代码说个xxx. show code!

2012/11/20 David pulq <pulq...@gmail.com>:

Chaos Eternal

unread,
Nov 20, 2012, 3:38:11 AM11/20/12
to sh...@googlegroups.com

依云

unread,
Nov 20, 2012, 3:42:24 AM11/20/12
to sh...@googlegroups.com
我来 show results 了(按从快到慢排列):

>>> time ./fib-c
./fib-c 2.40s user 0.00s system 99% cpu 2.405 total

>>> time python2 -c 'import fib_cython'
python2 -c 'import fib_cython' 2.41s user 0.01s system 99% cpu 2.421 total

>>> time python -c 'import fib_cython3'
python -c 'import fib_cython3' 2.67s user 0.00s system 99% cpu 2.677 total

>>> time java fib
java fib 4.85s user 0.01s system 99% cpu 4.879 total

>>> time ./fib-go
./fib-go 5.27s user 0.00s system 99% cpu 5.272 total

>>> time ./fib-hs
./fib-hs 6.61s user 0.00s system 99% cpu 6.613 total

>>> time racket fib-racket.scm
racket fib-racket.scm 10.86s user 0.04s system 99% cpu 10.998 total

>>> time luajit ./fib.lua
luajit ./fib.lua 16.77s user 0.00s system 99% cpu 16.772 total

timeout (> 20s):
lua, python2, guile, erl, ruby

David pulq

unread,
Nov 20, 2012, 3:48:01 AM11/20/12
to sh...@googlegroups.com
跟你这个差不多,只是写得丑一点。
话说这个测试就是比较各种语言/编译器/解释器对递归的优化, 做个加法什么语言跑起来速度都那样。

Chaos Eternal

unread,
Nov 20, 2012, 3:56:34 AM11/20/12
to sh...@googlegroups.com
再丑也得上代码。
我的代码已经很丑了。

2012/11/20 David pulq <pulq...@gmail.com>:
> 跟你这个差不多,只是写得丑一点。
> 话说这个测试就是比较各种语言/编译器/解释器对递归的优化, 做个加法什么语言跑起来速度都那样。

拿迭代跟递归比当然没啥好比,但是这个代码没有被自动优化的可能性(除了上面某楼说的那个很仙的办法),所以测的是*函数调用*的速度,不是*递归*的速度。

当然,shell的这个目的本身有很多槽点,回头慢慢吐之。

David pulq

unread,
Nov 20, 2012, 3:59:33 AM11/20/12
to sh...@googlegroups.com
#include<stdlib.h>
#include<stdio.h>
#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)

static long STACK[2];
static long do_fib_s(int n)
{
        int i;
        STACK[0] = 0;
        STACK[1] = 1;
        for (i=2; i < n+1; i++) {
                if (unlikely(i < 3)) {
                        STACK[i & 0x1] = 1;
                } else {
                        STACK[i & 1] = STACK[(i - 1) & 0x1] + STACK[(i-2) & 0x1];
                }
        }
        return STACK[n & 0x1];
        
}

int main(int argc, char *argv[])
{
        //printf("fib:%ld\n", do_fib(45));
        printf("fib_s:%ld\n", do_fib_s(45));
        return 0;

David pulq

unread,
Nov 20, 2012, 4:02:05 AM11/20/12
to sh...@googlegroups.com


On Tuesday, November 20, 2012 4:56:40 PM UTC+8, Chaos Eternal wrote:
再丑也得上代码。
我的代码已经很丑了。

2012/11/20 David pulq <pulq...@gmail.com>:
> 跟你这个差不多,只是写得丑一点。
> 话说这个测试就是比较各种语言/编译器/解释器对递归的优化, 做个加法什么语言跑起来速度都那样。

拿迭代跟递归比当然没啥好比,但是这个代码没有被自动优化的可能性(除了上面某楼说的那个很仙的办法),所以测的是*函数调用*的速度,不是*递归*的速度。
很久以前学生的时候记得尾递归是可以被编译器自动优化的,可惜忘记了具体条件了。

Chaos Eternal

unread,
Nov 20, 2012, 4:03:00 AM11/20/12
to sh...@googlegroups.com
条件就是 尾递归

但是shell的那个代码就不是尾递归的。

2012/11/20 David pulq <pulq...@gmail.com>:

David pulq

unread,
Nov 20, 2012, 4:03:43 AM11/20/12
to sh...@googlegroups.com
日,Google group里inline reply不好使了?
"很久以前学生的时候记得尾递归是可以被编译器自动优化的,可惜忘记了具体条件了。 "

Chaos Eternal

unread,
Nov 20, 2012, 4:06:31 AM11/20/12
to sh...@googlegroups.com
赞一下 STACK[i & 1] = STACK[(i - 1) & 0x1] + STACK[(i-2) &0x1]; 不知道跟
STACK[i & 1] = STACK[!(i & 0x1)] + STACK[i & 0x1]; 比那个更快。

吐一下那个if, 多余!

2012/11/20 David pulq <pulq...@gmail.com>:

David pulq

unread,
Nov 20, 2012, 4:10:27 AM11/20/12
to sh...@googlegroups.com
好吧,那个if是在递归版本里要来试一下unlikely的。后来忘了去掉了。

依云

unread,
Nov 20, 2012, 4:17:46 AM11/20/12
to sh...@googlegroups.com
On Tue, Nov 20, 2012 at 01:03:43AM -0800, David pulq wrote:
> 日,Google group里inline reply不好使了?
> "很久以前学生的时候记得尾递归是可以被编译器自动优化的,可惜忘记了具体条件了。 "

原来是行内回复坏掉了,我是说怎么经常在列表里看到整个邮件都是引用没一点正
文呢……

Chaos Eternal

unread,
Nov 20, 2012, 4:23:02 AM11/20/12
to sh...@googlegroups.com
我用这个逻辑放到我那个fib-gmp-opt里面去试了一下,貌似反而慢了。
现在不能push, 晚上再push。

2012/11/20 Chaos Eternal <chaose...@shlug.org>:

李晓岚

unread,
Nov 20, 2012, 4:50:33 AM11/20/12
to sh...@googlegroups.com
c++完胜,编译期间计算。
#include<stdio.h>
template<int v>
struct fib{
  enum{
    result = fib<v-1>::result + fib<v-2>::result,
  };
};
template<>struct fib<1>{
  enum{
    result = 1,
  };
};
template<>struct fib<2>{
  enum{
    result = 1,
  };
};
int main(){
  printf("%d\n",fib<45>::result);
  return 0;
}

--
不学无术



2012/11/20 Chaos Eternal <chaose...@shlug.org>

Chaos Eternal

unread,
Nov 20, 2012, 5:06:15 AM11/20/12
to sh...@googlegroups.com
瞎了。。。

chaos@felucia:~/tmp/spjb/performance/fib-iter$ \time g++
fibcpp.cpp -o fibcpp; \time ./fibcpp
0.03user 0.01system 0:00.21elapsed 23%CPU (0avgtext+0avgdata 35360maxresident)k
1520inputs+40outputs (6major+7010minor)pagefaults 0swaps
1134903170
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 1888maxresident)k
0inputs+0outputs (0major+157minor)pagefaults 0swaps


2012/11/20 李晓岚 <leexi...@gmail.com>:

Chaos Eternal

unread,
Nov 20, 2012, 5:07:39 AM11/20/12
to sh...@googlegroups.com
连编译在内一共0.3秒不到。g++威武。。

2012/11/20 Chaos Eternal <chaose...@shlug.org>:

YiZhi Liu

unread,
Nov 20, 2012, 5:11:53 AM11/20/12
to shlug
弱问g++为啥那么快

Chaos Eternal

unread,
Nov 20, 2012, 5:13:02 AM11/20/12
to sh...@googlegroups.com
因为没有栈

2012/11/20 YiZhi Liu <jave...@gmail.com>:

YiZhi Liu

unread,
Nov 20, 2012, 5:15:46 AM11/20/12
to shlug
我是说g++编译为什么那么快。。

Chaos Eternal

unread,
Nov 20, 2012, 5:37:52 AM11/20/12
to sh...@googlegroups.com

你觉得g++有可能每看到一个typedef就递归一次?
显然还是迭代啊。

没有栈

Shell Xu

unread,
Nov 20, 2012, 6:30:39 AM11/20/12
to sh...@googlegroups.com
写成尾递归也不算麻烦啊,最高貌似可以到O(lgn),问题是就失去测试的意义了。
无能者无所求,饱食而遨游,泛若不系之舟
blog: http://shell909090.com/blog/
twitter: http://twitter.com/shell909090

Chaos Eternal

unread,
Nov 20, 2012, 6:48:01 AM11/20/12
to sh...@googlegroups.com

如果有这样一种语言,自动把代码改写成尾递归,然后再按尾递归进行优化,就可以在本轮胜出。

David pulq

unread,
Nov 20, 2012, 8:43:39 AM11/20/12
to sh...@googlegroups.com
俺改了,不过效果不明显~

Xidorn Quan

unread,
Nov 20, 2012, 10:19:26 AM11/20/12
to sh...@googlegroups.com
如果有这样一种编译器,可以将递归改为递推
如果还有一个编译器,能够将递推式转化为通项公式……


2012/11/20 Chaos Eternal <chaose...@shlug.org>

Chaos Eternal

unread,
Nov 20, 2012, 10:49:52 AM11/20/12
to sh...@googlegroups.com
后面那个已经有了,前面那个比较难

2012/11/20 Xidorn Quan <quanx...@gmail.com>:

依云

unread,
Nov 20, 2012, 11:05:36 AM11/20/12
to sh...@googlegroups.com
On Tue, Nov 20, 2012 at 11:19:26PM +0800, Xidorn Quan wrote:
> 如果有这样一种编译器,可以将递归改为递推
> 如果还有一个编译器,能够将递推式转化为通项公式……

fib 的通匪公式涉及无理数啊,你还需要符号计算,不然不精确怎么办?

Xidorn Quan

unread,
Nov 20, 2012, 11:35:06 AM11/20/12
to sh...@googlegroups.com

反正结果是整数你随便取个整不就好了……

Ma Xiaojun

unread,
Nov 20, 2012, 11:59:48 AM11/20/12
to sh...@googlegroups.com
On Tue, Nov 20, 2012 at 10:05 AM, 依云 <lily...@gmail.com> wrote:
> fib 的通匪公式涉及无理数啊,你还需要符号计算,不然不精确怎么办?

不是有矩阵版……
http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form

Xidorn Quan

unread,
Nov 20, 2012, 6:10:49 PM11/20/12
to sh...@googlegroups.com

根本没解决无理数的问题

Ma Xiaojun

unread,
Nov 20, 2012, 7:07:45 PM11/20/12
to sh...@googlegroups.com
On Tue, Nov 20, 2012 at 5:10 PM, Xidorn Quan <quanx...@gmail.com> wrote:
> 根本没解决无理数的问题

怎麼沒解決,你算一個整數組成的矩陣的乘法需要無理數?

Xidorn Quan

unread,
Nov 20, 2012, 8:14:07 PM11/20/12
to sh...@googlegroups.com
好吧解决了


依云

unread,
Nov 20, 2012, 9:21:52 PM11/20/12
to sh...@googlegroups.com
这个好!

为什么这些有意思的东西课堂都提都不提呢?

tlze

unread,
Nov 20, 2012, 9:26:12 PM11/20/12
to sh...@googlegroups.com
On Wed, Nov 21, 2012 at 10:21 AM, 依云 <lily...@gmail.com> wrote:
> On Tue, Nov 20, 2012 at 10:59:48AM -0600, Ma Xiaojun wrote:
>> On Tue, Nov 20, 2012 at 10:05 AM, 依云 <lily...@gmail.com> wrote:
>> > fib 的通匪公式涉及无理数啊,你还需要符号计算,不然不精确怎么办?
>>
>> 不是有矩阵版……
>> http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form
>
> 这个好!
>
> 为什么这些有意思的东西课堂都提都不提呢?
>
> --
> Best regards,
> lilydjwg
>

这个答案好像给我发现了,因为考试时没有这题啊!

Liu Cheng

unread,
Nov 20, 2012, 9:26:56 PM11/20/12
to sh...@googlegroups.com
这就是线性递推式的logn的计算方法啊。 用A^n的快速算法...... 这种东西知道的老师都不多把

依云

unread,
Nov 20, 2012, 9:44:08 PM11/20/12
to sh...@googlegroups.com
仔细看了看,没发现有不涉及无理数的通项公式啊,那个矩阵形式还是递推式。

On Wed, Nov 21, 2012 at 09:14:07AM +0800, Xidorn Quan wrote:
> 好吧解决了
>
>
> On Wed, Nov 21, 2012 at 8:07 AM, Ma Xiaojun <damag...@gmail.com> wrote:
>
> > On Tue, Nov 20, 2012 at 5:10 PM, Xidorn Quan <quanx...@gmail.com>
> > wrote:
> > > 根本没解决无理数的问题
> >
> > 怎麼沒解決,你算一個整數組成的矩陣的乘法需要無理數?

--

Xidorn Quan

unread,
Nov 20, 2012, 10:03:49 PM11/20/12
to sh...@googlegroups.com

(1 1 ; 1 0) ^ n = (f(n+1) f(n) ; f(n) f(n+1))

在 2012-11-21 上午10:44,"依云" <lily...@gmail.com>写道:

Xidorn Quan

unread,
Nov 20, 2012, 10:04:35 PM11/20/12
to sh...@googlegroups.com

手机没法撤销发送……最后一个是 f(n-1)

Chaos Eternal

unread,
Nov 20, 2012, 10:10:57 PM11/20/12
to sh...@googlegroups.com
那个无理数就是个记号,你可以像复数那样设计一个类 a*x+i*y ,
x,y都是有理数,i是多少无所谓,只要知道i**2=5即可,然后这个类同样构成一个域,可以做加减乘除。

另外,那个矩阵乘法是满足结合律的,所以计算 pow(A, 2n)可以先算 pow(A,n)然后 计算 pow(A,n)*pow(A,n) ;
A=(1 1; 1 0) , sicp里面讲了的,hiahiahia.

回头上代码。

Liu Cheng

unread,
Nov 20, 2012, 10:12:47 PM11/20/12
to sh...@googlegroups.com
矩阵那个已经相当于 Xn = A * Xn-1了,转换成通项有问题么...

依云

unread,
Nov 20, 2012, 10:12:39 PM11/20/12
to sh...@googlegroups.com
这个怎么解出 f(n) 呢?

依云

unread,
Nov 21, 2012, 1:44:56 AM11/21/12
to sh...@googlegroups.com
无理数啊。

Chaos Eternal 的想法不错,坐等代码 :-)

Liu Cheng

unread,
Nov 21, 2012, 3:25:16 AM11/21/12
to sh...@googlegroups.com
贴个矩阵版logn的
def mul(a, b):
c = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
c[i][j] += a[i][k] * b[k][j]
return c

def fib(n):
if n <= 2: return 1
a = [[0, 1], [1, 1]]
y = [[1, 0], [0, 1]]
while n > 0 :
if n % 2 == 1:
y = mul(y, a)
a = mul(a, a)
n /= 2
return y[0][1]


n = 1000000
print "fib[%d]=%d" % (n, fib(n))

Chaos Eternal

unread,
Nov 21, 2012, 3:41:53 AM11/21/12
to sh...@googlegroups.com

Chaos Eternal

unread,
Nov 21, 2012, 4:07:19 AM11/21/12
to sh...@googlegroups.com
先贴一个sicp的作业, fib-fast

(define (fib-fast nc)
(define (iter a b c d n)
(cond ((= n 0) b)
((even? n) (iter a
b
(+ (* c c) (* d d))
(+ (* d d) (* 2 c d))
(/ n 2)))
(#t (iter (+ (* b d) (* a d) (* a c))
(+ (* b c) (* a d))
c
d
(- n 1)))))
(iter 1 0 0 1 nc))


On Wed, Nov 21, 2012 at 2:44 PM, 依云 <lily...@gmail.com> wrote:

Chaos Eternal

unread,
Nov 21, 2012, 4:07:52 AM11/21/12
to sh...@googlegroups.com
是sicp exercise 1.19

Chaos Eternal

unread,
Nov 28, 2012, 8:24:54 AM11/28/12
to sh...@googlegroups.com
代码在此!

(import (rnrs))

(define (mul-fibpair x y)
  (cons (+ (* (car x) (car y))
           (* 5 (cdr x) (cdr y)))
        (+ (* (car x) (cdr y))
           (* (cdr x) (car y)))))


(define (square-fibpair x) (mul-fibpair x x))

(define (fast-pow-fib b o n)
  (cond ((= n 0) o)
        ((even? n) (fast-pow-fib (square-fibpair b) o (/ n 2)))
        (#t (fast-pow-fib b (mul-fibpair b o) (- n 1)))))

(display (* 2 (cdr (fast-pow-fib '(1/2 . 1/2) '(1 . 0) 1000000))))



On Wed, Nov 21, 2012 at 2:44 PM, 依云 <lily...@gmail.com> wrote:

Chaos Eternal

unread,
Nov 28, 2012, 8:28:40 AM11/28/12
to sh...@googlegroups.com
说明如下:
用公式 Fn=2/sqrt(5)*((1/2+sqrt(5)/2)**n - (1/2-sqrt(5)/2)**n)

p4n.foo

unread,
Nov 28, 2012, 9:27:56 AM11/28/12
to shlug
用带无理数的通项公式会出问题的。
当初OJ上机的时候就有过斐波那契的题,不能用通项公式,大数有误差。
 

付鹏
北京航空航天大学沙河校区一公寓北322
Tel :18810662161
 
发件人: Chaos Eternal
发送时间: 2012-11-28 21:28
主题: Re: [shlug] 求助:c程序的效率比java还慢

Chaos Eternal

unread,
Nov 28, 2012, 9:33:25 AM11/28/12
to sh...@googlegroups.com
先看代码,看完准你叫一声大神。

Shell Xu

unread,
Nov 28, 2012, 9:57:49 AM11/28/12
to sh...@googlegroups.com
是sicp练习1.19吧?

2012/11/28 Chaos Eternal <chaose...@shlug.org>



--

Chaos Eternal

unread,
Nov 28, 2012, 10:13:15 AM11/28/12
to sh...@googlegroups.com
1.19那个作业我已经贴过了。


2012/11/28 Shell Xu <shell...@gmail.com>

p4n.foo

unread,
Nov 28, 2012, 10:14:18 AM11/28/12
to shlug
给跪了。。。
 

付鹏
北京航空航天大学沙河校区一公寓北322
Tel :18810662161
 
发件人: Shell Xu
发送时间: 2012-11-28 22:57
收件人: shlug
主题: Re: Re: [shlug] 求助:c程序的效率比java还慢

依云

unread,
Nov 28, 2012, 10:40:40 AM11/28/12
to sh...@googlegroups.com
递归呢。我所理解的「通项公式」是 A_n = f(n) 的非递归表示式。

请问怎么执行?guile 表示它不认识 import,删掉后才行。

Chaos Eternal

unread,
Nov 28, 2012, 10:42:53 AM11/28/12
to sh...@googlegroups.com
ikarus --r6rs-script fib-fast2.scm

依云

unread,
Nov 28, 2012, 12:06:29 PM11/28/12
to sh...@googlegroups.com
谢。不过这东西在 AUR 里,懒得编译了。用 guile 跑也挺快的嘛。

Chaos Eternal

unread,
Jan 3, 2018, 2:29:11 AM1/3/18
to sh...@googlegroups.com
挖个坟,
写了个scheme 宏,一样也是编译器展开
(define-syntax fib
  (lambda (x)
    (syntax-case x ()
      ((fib n)
       #'(fib n 1 0))
      ((fib 0 num2 num3) (syntax->datum #'num2))
      ((fib num1 num2 num3)
       (with-syntax ((n (datum->syntax #'fib
       (- (syntax->datum #'num1)
  1)))
     (n2 (datum->syntax #'fib
(+ (syntax->datum #'num2)
   (syntax->datum #'num3))))
     (n3 (syntax num2)))
#'(fib n n2 n3))))))

(fib 500)
;;=> 225591516161936330872512695036072072046011324913758190588638866418474627738686883405015987052796968498626


On Tue, Nov 20, 2012 at 5:51 PM 李晓岚 <leexi...@gmail.com> wrote:
c++完胜,编译期间计算。
#include<stdio.h>
template<int v>
struct fib{
  enum{
    result = fib<v-1>::result + fib<v-2>::result,
  };
};
template<>struct fib<1>{
  enum{
    result = 1,
  };
};
template<>struct fib<2>{
  enum{
    result = 1,
  };
};
int main(){
  printf("%d\n",fib<45>::result);
  return 0;
}

--
不学无术



2012/11/20 Chaos Eternal <chaose...@shlug.org>
我用这个逻辑放到我那个fib-gmp-opt里面去试了一下,貌似反而慢了。
现在不能push, 晚上再push。

2012/11/20 Chaos Eternal <chaose...@shlug.org>:
> 赞一下  STACK[i & 1] = STACK[(i - 1) & 0x1] + STACK[(i-2) &0x1]; 不知道跟
> STACK[i & 1] = STACK[!(i  & 0x1)] + STACK[i & 0x1]; 比那个更快。
>
> 吐一下那个if, 多余!
>
> 2012/11/20 David pulq <pulq...@gmail.com>:
>> #include<stdlib.h>
>> #include<stdio.h>
>> #define likely(x)       __builtin_expect((x),1)
>> #define unlikely(x)     __builtin_expect((x),0)
>>
>> static long STACK[2];
>> static long do_fib_s(int n)
>> {
>>         int i;
>>         STACK[0] = 0;
>>         STACK[1] = 1;
>>         for (i=2; i < n+1; i++) {
>>                 if (unlikely(i < 3)) {
>>                         STACK[i & 0x1] = 1;
>>                 } else {
>>                         STACK[i & 1] = STACK[(i - 1) & 0x1] + STACK[(i-2) &
>> 0x1];
>>                 }
>>         }
>>         return STACK[n & 0x1];
>>
>> }
>>
>> int main(int argc, char *argv[])
>> {
>>         //printf("fib:%ld\n", do_fib(45));
>>         printf("fib_s:%ld\n", do_fib_s(45));
>>         return 0;
>> }
>>
>>
>> On Tuesday, November 20, 2012 4:56:40 PM UTC+8, Chaos Eternal wrote:
>>>
>>> 再丑也得上代码。
>>> 我的代码已经很丑了。
>>>
>>> 2012/11/20 David pulq <pulq...@gmail.com>:
>>> > 跟你这个差不多,只是写得丑一点。
>>> > 话说这个测试就是比较各种语言/编译器/解释器对递归的优化, 做个加法什么语言跑起来速度都那样。
>>>
>>> 拿迭代跟递归比当然没啥好比,但是这个代码没有被自动优化的可能性(除了上面某楼说的那个很仙的办法),所以测的是*函数调用*的速度,不是*递归*的速度。
>>>
>>> 当然,shell的这个目的本身有很多槽点,回头慢慢吐之。
>>>
>>> >
>>> >
>>> > On Tuesday, November 20, 2012 4:38:15 PM UTC+8, Chaos Eternal wrote:
>>> >>
>>> >> https://gitcafe.com/chaoseternal/performance/tree/master/fib-iter
>>> >>
>>> >> 跑我这个版本吧。
>>> >>
>>> >>
>>> >> 2012/11/20 依云 <lily...@gmail.com>:
>>> >> >>>> time java fib
>>> >> > 1134903170
>>> >> > java fib  4.85s user 0.01s system 99% cpu 4.879 total
>>> >> >>>> time ./fib-c
>>> >> > 1134903170
>>> >> > ./fib-c  2.40s user 0.00s system 99% cpu 2.405 total
>>> >> >
>>> >> > 另外,同讨厌引用字符不规矩的 :-(
>>> >> >
>>> >> > On Tue, Nov 20, 2012 at 01:54:40PM +0800, Chaos Eternal wrote:
>>> >> >> 看了一下,除了是32位的,好像看不出啥。
>>> >> >>
>>> >> >> 另外,能不能让你的mutt在回复邮件的时候,引用字符改成 > 啊。。。
>>> >> >>
>>> >> >> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:
>>> >> >> >
>>> >> >> > gcc -S 的结果在附件
>>> >> >> >
>>> >> >> >
>>> >> >> > On Tue, Nov 20, 2012 at 12:49:55PM +0800, Chaos Eternal wrote:
>>> >> >> > --Date: Tue, 20 Nov 2012 12:49:55 +0800
>>> >> >> > --From: Chaos Eternal <chaose...@shlug.org>
>>> >> >> > --To: sh...@googlegroups.com
>>> >> >> > --Subject: Re: [shlug] 求助:c程序的效率比java还慢
>>> >> >> > --
>>> >> >> > --放大招
>>> >> >> > --gcc -s
>>> >> >> > --然后上结果。
>>> >> >> > --
>>> >> >> > --
>>> >> >> > --2012/11/20 Raymond Lau <ray...@uniteweb.net>:
>>> >> >> > -->
>>> >> >> > --> 最近网络一直都封了22端口,一直没能push到gitcafe上去。
>>> >> >> > --> 晚上我找个地方先push上去。
>>> >> >> > -->
>>> >> >> > --> 我也知道多半就是gcc环境的问题了。这些时放狗过几次,一直没有头绪。
>>> >> >> > -->
>>> >> >> > --> 所以看看slug里有木有C方面的大牛  ,能帮忙解决下看看。
>>> >> >> > -->
>>> >> >> > --> PS:楼上机器老给力了。java跑3.6s.. 说实话,java 能跑到5s我都老满足了。。
>>> >> >> > -->
>>> >> >> > --> On Tue, Nov 20, 2012 at 11:51:30AM +0800, Chaos Eternal wrote:
>>> >> >> > --> --Date: Tue, 20 Nov 2012 11:51:30 +0800
>>> >> >> > --> --From: Chaos Eternal <chaose...@shlug.org>
>>> >> >> > --> --To: sh...@googlegroups.com
>>> >> >> > --> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
>>> >> >> > --> --
>>> >> >> > --> --拿你的代码我测了一下,
>>> >> >> > --> --java跑3.60
>>> >> >> > --> --C跑3.70
>>> >> >> > --> --
>>> >> >> > --> --相差不大。
>>> >> >> > --> --
>>> >> >> > --> --看来确实是你的环境问题。
>>> >> >> > --> --
>>> >> >> > --> --
>>> >> >> > --> --2012/11/20 Chaos Eternal <chaose...@shlug.org>:
>>> >> >> > --> --> 那个程序我跑只要3秒,为了消除硬件、环境的差异,所以需要你的代码。最好还有makefile。
>>> >> >> > --> -->
>>> >> >> > --> --> 2012/11/20 Raymond Lau <ray...@uniteweb.net>:
>>> >> >> > --> -->> java 代码:
>>> >> >> > --> -->> public class fib {
>>> >> >> > --> -->>
>>> >> >> > --> -->>         static public long do_fib(final int n) {
>>> >> >> > --> -->>                 if (n < 3)
>>> >> >> > --> -->>                         return 1;
>>> >> >> > --> -->>                 return do_fib(n - 1) + do_fib(n - 2);
>>> >> >> > --> -->>         }
>>> >> >> > --> -->>
>>> >> >> > --> -->>         /**
>>> >> >> > --> -->>          * @param args
>>> >> >> > --> -->>          */
>>> >> >> > --> -->>         public static void main(String[] args) {
>>> >> >> > --> -->>                 System.out.println(do_fib(45));
>>> >> >> > --> -->>         }
>>> >> >> > --> -->>
>>> >> >> > --> -->> }
>>> >> >> > --> -->> 窃以为,不是java代码的问题。
>>> >> >> > --> -->> C程序的runtime时间也太长了,12s ..太夸张了些。比Shell的3.773 s相差太远。
>>> >> >> > --> -->>
>>> >> >> > --> -->> 另外在parse这个问题里,c的实现是原生的grep..
>>> >> >> > 测试这个问题时,我的结果与Shell的结果相关不大,比起java,go来确实是要高出一大截。
>>> >> >> > --> -->>
>>> >> >> > --> -->> 所以我还是怀疑是我gcc 编译的问题。。
>>> >> >> > --> -->>
>>> >> >> > --> -->>
>>> >> >> > --> -->> On Tue, Nov 20, 2012 at 11:06:57AM +0800, Chaos Eternal
>>> >> >> > wrote:
>>> >> >> > --> -->> --Date: Tue, 20 Nov 2012 11:06:57 +0800
>>> >> >> > --> -->> --From: Chaos Eternal <chaose...@shlug.org>
>>> >> >> > --> -->> --To: sh...@googlegroups.com
>>> >> >> > --> -->> --Subject: Re: [shlug] 求助:c程序的效率比java还慢
>>> >> >> > --> -->> --
>>> >> >> > --> -->> --java代码拿来。
>>> >> >> > --> -->> --
>>> >> >> > --> -->> --
>>> >> >> > --> -->> --On Tue, Nov 20, 2012 at 7:00 PM, Raymond Lau
>>> >> >> > <ray...@uniteweb.net> wrote:
>>> >> >> > --> -->> --> ---背景
>>> >> >> > --> -->> --> 本来这个话题是想在 hacking
>>> >> >> > 上讨论的。只为这2次周四都较忙,没有空去,所以就在slug里讨论一下了。
>>> >> >> > --> -->> -->
>>> >> >> >
>>> >> >> > 这个事情是这样的,在某个周四的晚上与Shell交流时,对他的performance项目(https://gitcafe.com/shell909090/performance)挺感兴趣的
>>> >> >> > --> -->> --> 所以fork了一个,然后很快用java 实现了fibonacci。
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> 再然后,在我的机器上,测下来的结果与Shell公布的结果相差很大,特别是C :
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> make  c
>>> >> >> > --> -->> --> ../perf -l c ./fib-c
>>> >> >> > --> -->> --> lang: c, filesize: 4 kB, runtime: 12.017 s, memory:
>>> >> >> > 392
>>> >> >> > kB
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> Shell的C的结果是:
>>> >> >> > --> -->> --> +---------+----------+---------+---------+--------+
>>> >> >> > --> -->> --> |   lang  | filesize | runtime |  memory | remark |
>>> >> >> > --> -->> --> +---------+----------+---------+---------+--------+
>>> >> >> > --> -->> --> |    c    |   6.0    |  3.773  |  496.0  |        |
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> 甚至比java混全模式慢上一倍多:
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> make   java-server
>>> >> >> > --> -->> --> javac   fib.java
>>> >> >> > --> -->> --> ../perf -l java-server java -server fib
>>> >> >> > --> -->> --> lang: java-server, filesize: 34 kB, runtime: 5.824 s,
>>> >> >> > memory: 15376 kB
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> 甚至与go的效率差不多:
>>> >> >> > --> -->> -->  make go
>>> >> >> > --> -->> --> ../perf -l go ./fib-go
>>> >> >> > --> -->> --> lang: go, filesize: 1142 kB, runtime: 13.728 s,
>>> >> >> > memory:
>>> >> >> > 852 kB
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> Shell公布go的结果是:
>>> >> >> > --> -->> --> |    go   |  1242.0  |  7.441  |  880.0  |        |
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> ---问题:
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> 好吧,背景有些长,问题来了:
>>> >> >> > --> -->> --> 我知道这应该是gcc的问题,但是却不知道具体是gcc的神马问题?有木有玩C的大侠解解惑?
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> ps:
>>> >> >> > --> -->> --> fib.c的编译参数
>>> >> >> > --> -->> -->     gcc -O2 -o
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> gcc 版本:
>>> >> >> > --> -->> -->     gcc  -v
>>> >> >> > --> -->> -->     使用内建 specs。
>>> >> >> > --> -->> -->     COLLECT_GCC=gcc
>>> >> >> > --> -->> -->
>>> >> >> >
>>> >> >> > COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/lto-wrapper
>>> >> >> > --> -->> -->     目标:i686-pc-linux-gnu
>>> >> >> > --> -->> -->     配置为:/build/src/gcc-4.7-20120721/configure
>>> >> >> > --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
>>> >> >> > --mandir=/usr/share/man --infodir=/usr/share/info
>>> >> >> > --with-bugurl=https://bugs.archlinux.org/
>>> >> >> > --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++
>>> >> >> > --enable-shared
>>> >> >> > --enable-threads=posix --with-system-zlib --enable-__cxa_atexit
>>> >> >> > --disable-libunwind-exceptions --enable-clocale=gnu
>>> >> >> > --disable-libstdcxx-pch
>>> >> >> > --enable-libstdcxx-time --enable-gnu-unique-object
>>> >> >> > --enable-linker-build-id
>>> >> >> > --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check
>>> >> >> > --disable-cloog-version-check --enable-lto --enable-gold
>>> >> >> > --enable-ld=default
>>> >> >> > --enable-plugin --with-plugin-ld=ld.gold
>>> >> >> > --with-linker-hash-style=gnu
>>> >> >> > --disable-multilib --disable-libssp --disable-build-with-cxx
>>> >> >> > --disable-build-poststage1-with-cxx --enable-checking=release
>>> >> >> > --> -->> -->     线程模型:posix
>>> >> >> > --> -->> -->     gcc 版本 4.7.1 20120721 (prerelease) (GCC)
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> java版本:
>>> >> >> > --> -->> -->     java -version
>>> >> >> > --> -->> -->     java version "1.6.0_24"
>>> >> >> > --> -->> -->     OpenJDK Runtime Environment (IcedTea6 1.11.4)
>>> >> >> > (ArchLinux-6.b24_1.11.4-1-i686)
>>> >> >> > --> -->> -->     OpenJDK Server VM (build 20.0-b12, mixed mode)
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> kernel版本:
>>> >> >> > --> -->> -->      uname -a
>>> >> >> > --> -->> -->     Linux uniteweb001 3.5.3-1-pae #1 SMP PREEMPT Sun
>>> >> >> > Sep
>>> >> >> > 9 18:09:00 UTC 2012 i686 GNU/Linux
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> 全是用的archlinux库里的。
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --> --
>>> >> >> > --> -->> --> -- You received this message because you are

>>> >> >> > subscribed
>>> >> >> > to the Google Groups Shanghai Linux User Group group. To post to
>>> >> >> > this group,
>>> >> >> > send email to sh...@googlegroups.com. To unsubscribe from this
>>> >> >> > group, send
>>> >> >> > email to shlug+un...@googlegroups.com. For more options, visit
>>> >> >> > this group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> -->
>>> >> >> > --> -->> --
>>> >> >> > --> -->> ----
>>> >> >> > --> -->> ---- You received this message because you are subscribed

>>> >> >> > to
>>> >> >> > the Google Groups Shanghai Linux User Group group. To post to this
>>> >> >> > group,
>>> >> >> > send email to sh...@googlegroups.com. To unsubscribe from this
>>> >> >> > group, send
>>> >> >> > email to shlug+un...@googlegroups.com. For more options, visit
>>> >> >> > this group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > --> -->> --
>>> >> >> > --> -->> --
>>> >> >> > --> -->>
>>> >> >> > --> -->> --
>>> >> >> > --> -->> Raymond Lau
>>> >> >> > --> -->>
>>> >> >> > --> -->> --
>>> >> >> > --> -->> -- You received this message because you are subscribed

>>> >> >> > to
>>> >> >> > the Google Groups Shanghai Linux User Group group. To post to this
>>> >> >> > group,
>>> >> >> > send email to sh...@googlegroups.com. To unsubscribe from this
>>> >> >> > group, send
>>> >> >> > email to shlug+un...@googlegroups.com. For more options, visit
>>> >> >> > this group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > --> -->>
>>> >> >> > --> -->>
>>> >> >> > --> --
>>> >> >> > --> ----
>>> >> >> > --> ---- You received this message because you are subscribed to

>>> >> >> > the
>>> >> >> > Google Groups Shanghai Linux User Group group. To post to this
>>> >> >> > group, send
>>> >> >> > email to sh...@googlegroups.com. To unsubscribe from this group,
>>> >> >> > send email
>>> >> >> > to shlug+un...@googlegroups.com. For more options, visit this
>>> >> >> > group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > --> --
>>> >> >> > --> --
>>> >> >> > -->
>>> >> >> > --> --
>>> >> >> > --> Raymond Lau
>>> >> >> > -->
>>> >> >> > --> --

>>> >> >> > --> -- You received this message because you are subscribed to the
>>> >> >> > Google Groups Shanghai Linux User Group group. To post to this
>>> >> >> > group, send
>>> >> >> > email to sh...@googlegroups.com. To unsubscribe from this group,
>>> >> >> > send email
>>> >> >> > to shlug+un...@googlegroups.com. For more options, visit this
>>> >> >> > group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > -->
>>> >> >> > -->
>>> >> >> > --
>>> >> >> > ----
>>> >> >> > ---- You received this message because you are subscribed to the

>>> >> >> > Google Groups Shanghai Linux User Group group. To post to this
>>> >> >> > group, send
>>> >> >> > email to sh...@googlegroups.com. To unsubscribe from this group,
>>> >> >> > send email
>>> >> >> > to shlug+un...@googlegroups.com. For more options, visit this
>>> >> >> > group at
>>> >> >> > https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> >> >> > --
>>> >> >> > --
>>> >> >> >
>>> >> >> > --
>>> >> >> > Raymond Lau
Reply all
Reply to author
Forward
0 new messages