2012-03-01 86 views
5

我正在研究一些在Ruby中以不同格式解析時間值的文本轉換例程。這個例程越來越複雜,我目前正在測試更好的方法來解決這個問題。爲什麼ruby scanf太慢了?

我目前正在測試一種使用方法scanf。爲什麼?我總是認爲比正則表達式快,但是Ruby中發生了什麼?這太慢了!

我在做什麼錯?

注:我使用的紅寶石1.9.2-P290 [x86_64的(MRI)

第一臺紅寶石測試:

require "scanf" 
require 'benchmark' 

def duration_in_seconds_regex(duration) 
    if duration =~ /^\d{2,}\:\d{2}:\d{2}$/ 
    h, m, s = duration.split(":").map{ |n| n.to_i } 
    h * 3600 + m * 60 + s 
    end 
end 

def duration_in_seconds_scanf(duration) 
    a = duration.scanf("%d:%d:%d") 
    a[0] * 3600 + a[1] * 60 + a[2] 
end 

n = 500000 
Benchmark.bm do |x| 
    x.report { for i in 1..n; duration_in_seconds_scanf("00:10:30"); end } 
end 

Benchmark.bm do |x| 
    x.report { for i in 1..n; duration_in_seconds_regex("00:10:30"); end } 
end 

這是我使用scanf第一和第二的正則表達式:

 user  system  total  real 
    95.020000 0.280000 95.300000 (96.364077) 
     user  system  total  real 
    2.820000 0.000000 2.820000 ( 2.835170) 

第二測試使用C:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <sys/types.h> 
#include <string.h> 
#include <regex.h> 

char *regexp(char *string, char *patrn, int *begin, int *end) { 
    int i, w = 0, len; 
    char *word = NULL; 
    regex_t rgT; 
    regmatch_t match; 
    regcomp(&rgT, patrn, REG_EXTENDED); 
    if ((regexec(&rgT, string, 1, &match, 0)) == 0) { 
     *begin = (int) match.rm_so; 
     *end = (int) match.rm_eo; 
     len = *end - *begin; 
     word = malloc(len + 1); 
     for (i = *begin; i<*end; i++) { 
      word[w] = string[i]; 
      w++; 
     } 
     word[w] = 0; 
    } 
    regfree(&rgT); 
    return word; 
} 

int main(int argc, char** argv) { 
    char * str = "00:01:30"; 
    int h, m, s; 
    int i, b, e; 
    float start_time, end_time, time_elapsed; 
    regex_t regex; 
    regmatch_t * pmatch; 
    char msgbuf[100]; 
    char *pch; 
    char *str2; 
    char delims[] = ":"; 
    char *result = NULL; 

    start_time = (float) clock()/CLOCKS_PER_SEC; 
    for (i = 0; i < 500000; i++) { 
     if (sscanf(str, "%d:%d:%d", &h, &m, &s) == 3) { 
      s = h * 3600L + m * 60L + s; 
     } 
    } 
    end_time = (float) clock()/CLOCKS_PER_SEC; 
    time_elapsed = end_time - start_time; 
    printf("sscanf_time (500k iterations): %.4f", time_elapsed); 

    start_time = (float) clock()/CLOCKS_PER_SEC; 
    for (i = 0; i < 500000; i++) { 
     char * match = regexp(str, "[0-9]{2,}:[0-9]{2}:[0-9]{2}", &b, &e); 
     if (strcmp(match, str) == 0) { 
      str2 = (char*) malloc(sizeof (str)); 
      strcpy(str2, str); 
      h = strtok(str2, delims); 
      m = strtok(NULL, delims); 
      s = strtok(NULL, delims); 
      s = h * 3600L + m * 60L + s; 
     } 
    } 
    end_time = (float) clock()/CLOCKS_PER_SEC; 
    time_elapsed = end_time - start_time; 
    printf("\n\nregex_time (500k iterations): %.4f", time_elapsed); 

    return (EXIT_SUCCESS); 
} 

C代碼的結果是明顯加快,和正則表達式的結果是慢scanf結果不出所料:

sscanf_time (500k iterations): 0.1774 

regex_time (500k iterations): 3.9692 

很明顯,在C運行時間比較快,所以請不要評論說,Ruby是解釋和像這樣的東西請。

這是相關的gist

+0

難道你不是在C中每次迭代重新編譯表達式嗎?我不認爲Ruby這樣做。如果只編譯一次表達式,我會很有興趣看到C結果。另外,你爲什麼甚至使用拆分?您正在匹配字符串,因此您可以直接捕獲值,而無需對字符串進行進一步操作。 – Qtax 2012-03-01 20:35:55

+0

是的,我正在重新編譯,它可能比這更快,但我有時需要更改exp。 – AndreDurao 2012-03-01 20:41:40

+0

然後,只需在更改時重新編譯它即可。但我只想看看這些數字。 ;-) – Qtax 2012-03-01 20:45:31

回答

4

問題不在於它被解釋,而在於Ruby中的一切都是一個對象。您可以在Ruby分發中探索「scanf.rb」,並將其與scanf在C中的實現進行比較。

基於RegExp匹配的scanf的Ruby實現。像「%d」這樣的每個原子都是ruby中的一個對象,而C中只有一個例子。因此,在我看來,這種執行時間的原因是大量的對象分配/釋放。

+0

我認爲scanf使用本地實現就像openssl和其他需要.so文件 – AndreDurao 2012-03-01 20:49:52

2

假設MRI:scanf是用Ruby(scanf.rb)編寫的,顯然是10年前,從此以後從未接觸過(而且看起來很複雜!)。 split,map和正則表達式在大量優化的C中實現。