CSV 파일 처리 벤치마킹: Golang, NestJS, PHP, Python

WBOY
풀어 주다: 2024-08-12 22:34:02
원래의
895명이 탐색했습니다.

소개

대용량 CSV 파일을 효율적으로 처리하는 것은 데이터 분석부터 ETL(추출, 변환, 로드) 프로세스에 이르기까지 많은 애플리케이션에서 공통 요구 사항입니다. 이 기사에서는 MacBook Pro M1에서 대용량 CSV 파일을 처리할 때 인기 있는 4가지 프로그래밍 언어인 Golang, NodeJS와 NestJS, PHP 및 Python의 성능을 벤치마킹하고 싶습니다. 저는 어떤 언어가 이 작업에 가장 적합한 성능을 제공하는지 결정하는 것을 목표로 합니다.

테스트 환경

하드웨어: MacBook Pro M1, 256GB SSD, 8GB RAM

소프트웨어:

  • macOS Sonoma 14.5
  • PHP 8.3.6
  • 골랭 1.22.4
  • NestJS가 포함된 Node.js 22.0.0
  • 파이썬 3.12.3

테스트 데이터

sales_data.csv라는 합성 CSV 파일을 사용했는데, 각 행에는 transaction_id, product_id, 수량, 가격, 타임스탬프 등의 거래 세부정보가 포함되어 있으며 약 1백만 행이 있습니다.

작업 설명

각 언어에 대해 스크립트는 다음 작업을 수행합니다.

  1. CSV 파일을 읽습니다.
  2. 총 판매액을 계산합니다.
  3. 판매가 가장 높은 제품을 식별합니다.

구현

각 언어에 사용되는 스크립트는 다음과 같습니다.

골랭 스크립트:

sales.go

package main

import (
    "encoding/csv"
    "fmt"
    "os"
    "strconv"
    "time"
)

func main() {
    start := time.Now()

    file, err := os.Open("../generate-csv/sales_data.csv")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    defer file.Close()

    reader := csv.NewReader(file)
    _, _ = reader.Read() // Skip header

    totalSales := 0.0
    productSales := make(map[string]float64)

    for {
        line, err := reader.Read()
        if err != nil {
            break
        }
        productID := line[1]
        quantity, _ := strconv.Atoi(line[2])
        price, _ := strconv.ParseFloat(line[3], 64)
        total := float64(quantity) * price

        totalSales += total
        productSales[productID] += total
    }

    var topProduct string
    var topSales float64
    for product, sales := range productSales {
        if sales > topSales {
            topProduct = product
            topSales = sales
        }
    }

    elapsed := time.Since(start)
    fmt.Printf("Golang Execution time: %s\n", elapsed)
    fmt.Printf("Total Sales: $%.2f\n", totalSales)
    fmt.Printf("Top Product: %s with sales $%.2f\n", topProduct, topSales)
}

로그인 후 복사

NestJS 스크립트:

csv.service.ts

import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import * as fastcsv from 'fast-csv';

// path file CSV
const GLOBAL_CSV_PATH = '../generate-csv/sales_data.csv';

@Injectable()
@Injectable()
export class CsvService {
  async parseCsv(): Promise<{
    nestExecutionTime: number;
    totalSales: number;
    topProductSales: number;
  }> {
    return new Promise((resolve, reject) => {
      const startTime = process.hrtime();

      let totalSales = 0;
      const productSales: { [key: string]: number } = {};

      fs.createReadStream(GLOBAL_CSV_PATH)
        .pipe(fastcsv.parse({ headers: true, delimiter: ',' }))
        .on('data', (row) => {
          const productID = row.product_id;
          const quantity = parseInt(row.quantity, 10);
          const price = parseFloat(row.price);
          const total = quantity * price;
          totalSales += total;
          if (!productSales[productID]) {
            productSales[productID] = 0;
          }
          productSales[productID] += total;
        })
        .on('end', () => {
          const topProduct = Object.keys(productSales).reduce((a, b) =>
            productSales[a] > productSales[b] ? a : b,
          );
          const topProductSales = productSales[topProduct] || 0;
          const endTime = process.hrtime(startTime);
          const nestExecutionTime = endTime[0] + endTime[1] / 1e9;

          console.log(`NestJS Execution time: ${nestExecutionTime} seconds`);
          console.log(`Total Sales: $${totalSales}`);
          console.log(
            `Top Product: ${topProduct} with sales $${topProductSales}`,
          );

          resolve({
            nestExecutionTime,
            totalSales,
            topProductSales,
          });
        })
        .on('error', (error) => reject(error));
    });
  }
}
로그인 후 복사

csv.controller.ts

import { Controller, Get } from '@nestjs/common';
import { CsvService } from './csv.service';

@Controller('csv')
export class CsvController {
  constructor(private readonly csvService: CsvService) {}

  @Get('parse')
  async parseCsv(): Promise<{
    nestExecutionTime: number;
    totalSales: number;
    topProductSales: number;
  }> {
    return this.csvService.parseCsv();
  }
}
로그인 후 복사

PHP 스크립트

sales.php

<?php
$start_time = microtime(true);

$file = fopen("../generate-csv/sales_data.csv", "r");
$total_sales = 0;
$product_sales = [];

fgetcsv($file); // Skip header
while (($line = fgetcsv($file)) !== false) {
    $product_id = $line[1];
    $quantity = (int)$line[2];
    $price = (float)$line[3];
    $total = $quantity * $price;

    $total_sales += $total;
    if (!isset($product_sales[$product_id])) {
        $product_sales[$product_id] = 0;
    }
    $product_sales[$product_id] += $total;
}
fclose($file);

arsort($product_sales);
$top_product = array_key_first($product_sales);

$end_time = microtime(true);
$execution_time = ($end_time - $start_time);

echo "PHP Execution time: ".$execution_time." seconds\n";
echo "Total Sales: $".$total_sales."\n";
echo "Top Product: ".$top_product." with sales $".$product_sales[$top_product]."\n";
로그인 후 복사

파이썬 스크립트

import csv
import time

# Input file name config
input_file = '../generate-csv/sales_data.csv'


def parse_csv(file_path):
    start_time = time.time()

    total_sales = 0
    product_sales = {}

    with open(file_path, mode='r') as file:
        reader = csv.DictReader(file)

        for row in reader:
            product_id = row['product_id']
            quantity = int(row['quantity'])
            price = float(row['price'])
            total = quantity * price
            total_sales += total

            if product_id not in product_sales:
                product_sales[product_id] = 0
            product_sales[product_id] += total

    top_product = max(product_sales, key=product_sales.get)
    execution_time = time.time() - start_time

    return {
        'total_sales': total_sales,
        'top_product': top_product,
        'top_product_sales': product_sales[top_product],
        'execution_time': execution_time,
    }


if __name__ == "__main__":
    result = parse_csv(input_file)
    print(f"Python Execution time: {result['execution_time']:.2f} seconds")
    print(f"Total Sales: ${result['total_sales']:.2f}")
    print(f"Top Product: {result['top_product']} with sales ${
          result['top_product_sales']:.2f}")

로그인 후 복사

결과

벤치마크 테스트 결과는 다음과 같습니다.

골랑

  • 실행 시간: 466.69975ms
  • 총 매출: $274654985.36
  • 상위 제품: 제품 1126, 매출 $305922.81

Benchmarking CSV File Processing: Golang vs NestJS vs PHP vs Python

NestJS

  • 실행 시간: 6.730134208초
  • 총 매출: $274654985.36000216
  • 상위 제품: 1126, 매출 $305922.8099999997

Benchmarking CSV File Processing: Golang vs NestJS vs PHP vs Python

PHP

  • 실행 시간: 1.5142710208893초
  • 총 매출: $274654985.36
  • 최고 제품: 1126, 매출 $305922.81

Benchmarking CSV File Processing: Golang vs NestJS vs PHP vs Python

파이썬

  • 실행 시간: 2.56초
  • 총 매출: $274654985.36
  • 최고 제품: 1126, 매출 $305922.81

Benchmarking CSV File Processing: Golang vs NestJS vs PHP vs Python

분석

내 벤치마크에서는 몇 가지 흥미로운 통찰력을 보여줍니다.

실행 시간: 실행 시간 측면에서 Golang이 가장 좋았고 PHP8이 그 뒤를 이었습니다. NestJS는 작업을 완료하는 데 가장 오랜 시간이 걸렸습니다.
메모리 사용량: Build NestJS는 효율적인 메모리 사용량을 보인 반면 Python은 더 높은 메모리 사용량을 보여주었습니다.
구현 용이성: Golang은 가장 간단한 구현을 제공한 반면 NestJS는 더 많은 코드 라인과 복잡성을 요구했습니다.

결론

제가 조사한 바에 따르면 Golang은 최고의 성능 속도와 메모리 효율성을 제공하므로 대규모 데이터 세트를 처리하는 데 탁월한 선택입니다.

완전한 코드

내 Github 저장소에서 전체 코드를 얻을 수 있습니다
csv-파싱-전투.

위 내용은 CSV 파일 처리 벤치마킹: Golang, NestJS, PHP, Python의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!