C語言處理CSV數據

如下代碼爲博客html

《Python的並行求和例子》:python

http://www.cnblogs.com/instant7/p/4312786.htmllinux

中並行python代碼的C語言重寫版。ios

用C來跑一遍單線程也只須要50秒,比python 開4進程的實現要快6倍多,CPU佔用率也只用python的1/4。c++

看來計算密集型應用仍是須要用這些不順手的老古董來弄的:)函數

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

char *trim(char *str)
{
    char *p = str; 
    while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
        p ++;
    str = p; 
    p = str + strlen(str) - 1; 
    while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
        -- p;
    *(p + 1) = '\0'; 
    return str; 
}

int main(){
    cout<<"start time: ";
    system("Echo %Date% %Time%");
    FILE *fexp, *fpred;
    fexp = fopen("D:\\kaggle\\rain\\train_exp.csv", "r");
    fpred = fopen("D:\\kaggle\\rain\\trainChangeTimePeriod.csv","r");

    char expLine[10240];
    char predLine[10240];

    fgets(expLine, sizeof(expLine), fexp);
    fgets(predLine, sizeof(predLine), fpred);


    double squareErrorSum = 0;
    int rowCnt = 0;
    while(fgets(expLine, sizeof(expLine), fexp)) {
        //printf("%s", expLine);
        fgets(predLine, sizeof(predLine), fpred);
        //printf("%s", predLine);

        char *save_ptr;
        char *expId = trim(strtok_s(expLine, ",", &save_ptr));
        if (expId == NULL) {
            return -1;
        } 
        char *exp = trim(strtok_s(NULL, ",", &save_ptr));
        double expVal = atof(exp);
        //printf("%s\t%s\n", expId, exp);

        char *predId = trim(strtok_s(predLine, ",", &save_ptr));
        //printf("%s\n", predId);
        double prob[100];
        rowCnt += 1;
        if (rowCnt % 20000 == 0)
            cout << rowCnt << " finished"<<endl;
        for (int i = 0; i < 70; i++){
            char *temp = trim(strtok_s(NULL, ",", &save_ptr));
            prob[i] = atof(temp);
            squareErrorSum += pow((prob[i]-(i>=expVal)),2);
            //cout<<squareErrorSum<<endl;
        }
    }
    double score = squareErrorSum / (70 * rowCnt);
    cout<<"Score: "<<score<<endl;
    cout<<"end time: ";
    system("Echo %Date% %Time%");
    system("pause");
    return 0;
}

linux的話分割字符串函數要換成:spa

strtok_r

C/C++對CSV的處理參考自:.net

《c語言讀取csv文件和c++讀取csv文件示例分享》線程

http://www.jb51.net/article/47962.htmcode

相關文章
相關標籤/搜索