LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1050|回复: 4

PERL写的程序运行真的这么慢吗

[复制链接]
发表于 2004-5-20 09:37:48 | 显示全部楼层 |阅读模式
/*****
  PERL : perl-test.pl
******/
#!/usr/bin/perl -w
#
use DBI; # Load the DBI module
my $dbh = DBI->connect( "dbi:mysql:test","root", "", {
PrintError => 0,
RaiseError => 0
} ) or die "Can't connect to the database: $DBI::errstr\n";
### Prepare a SQL statement for execution
my $sth = $dbh->prepare( "SELECT * FROM user_name" )
or die "Can't prepare SQL statement: $DBI::errstr\n";
### Execute the statement in the database
$sth->execute
or die "Can't execute SQL statement: $DBI::errstr\n";
### Retrieve the returned rows of data
my @row;
#my $row_str;
while ( @row = $sth->fetchrow_array( ) ) {
#$row_str=join(",",@row);
print "Row: @row\n";
}
warn "Data fetching terminated early by error: $DBI::errstr\n"
if $DBI::err;
### Disconnect from the database
$dbh->disconnect
or warn "Error disconnecting: $DBI::errstr\n";
exit;

/**********
   C : test.c
***********/

#define MAXLEN 50
#include <stdio.h>
#include "mysql.h"
#include "mysqld_error.h"
#include <error.h>
#include <sys/errno.h>
int main(int argc ,char **argv)
{
        MYSQL mysql;
        MYSQL_RES *mysql_result;
        MYSQL_FIELD *field;
        MYSQL_ROW row;
        int count = 0;
        int i= 0;
        char    sQueue[MAXLEN + 1];

        memset(sQueue,0,sizeof sQueue);
        strcpy(sQueue,"select * from user_name");
        mysql_init(&mysql);
        if (!mysql_real_connect  (&mysql,"localhost","root","","test",0,NULL,0))
        {
            printf("Failed to connect to database: Error: %s\n",
            mysql_error(&mysql));
            exit(-1);
        }
        if(mysql_real_query(&mysql, sQueue, strlen(sQueue)))
        {
            printf("Failed to queue to database: Error: %s\n",
            mysql_error(&mysql));
            exit(-1);
        }
        mysql_result=mysql_store_result(&mysql);

        count=mysql_field_count(&mysql);
        while((row=mysql_fetch_row(mysql_result)))
        {
                for(;i<count;i++)
                        printf(" %s\t", row);
      i = 0;
                printf("\n");
        }

        mysql_close(&mysql);
        exit (0);
}

/*****
   Makefile FOR C  test.c:
*****/
INCL=/usr/include/mysql
CC=gcc
CFLAGS=-DDEBUG -g -c
#TARGET=../
%.o:%.c
        $(CC) $(CFLAGS) -I$(INCL) $<


all:test
test:test.o
        $(CC)  -o $@ $< -lmysqlclient -lm -lpthread -lcurses
        rm $@.o
        @echo


两个都是简单的显示mysql数据库TEST中表user_name的数据.
[root@localhost perl]# time perl perl-test.pl
Row: wwshao 0
Row: shao 1
Row: wwshao 0
Row: shao 1
Row: shao 0
Row: shaoww 1
Row: shaoww 0
Row: wwshao 1
Row: wwshao 0

real    0m0.537s
user    0m0.350s
sys     0m0.180s
[root@localhost mysql-examp]# time ./test
wwshao  0
shao    1
wwshao  0
shao    1
shao    0
shaoww  1
shaoww  0
wwshao  1
wwshao  0

real    0m0.091s
user    0m0.020s
sys     0m0.050s

以前只知道PERL比C慢,没想到差这么多....
各位兄弟有没有好的建议,是不是DBI模块的原因.我用的是最新的...
先谢过!
发表于 2004-5-20 11:00:46 | 显示全部楼层
数据库我不懂,但我觉得这样比是不公平的:perl的初始化一向很慢,只运行一次的话它的时间都花在初始化上了。

请参考DBI模块文档中的Performance那一节。把你的程序改成连续读1000次(或10000次)数据,看看这样的话它和C差多少。
 楼主| 发表于 2004-5-20 13:35:06 | 显示全部楼层
谢谢
连续读表数据1000次的结果.
perl :
real    0m5.610s
user    0m1.860s
sys     0m0.850s

c:
real    0m4.958s
user    0m1.330s
sys     0m1.190s
发表于 2004-5-21 08:54:19 | 显示全部楼层
慢是正常的吧!你想想perl要把多少module都放在link裡面??
即便沒有用..他也會link and compile...
用perlcc compile過後..binary文件大小絕對比c 的大很多.
所以慢是正常的...
c是出了名的快...
這樣比較沒有意義吧...
perl的優點是?椥...
 楼主| 发表于 2004-5-21 10:21:42 | 显示全部楼层
谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表