LoginSignup
44
44

More than 5 years have passed since last update.

【言語別】複数行の標準入力→int型配列 の高速化(Tips)

Last updated at Posted at 2014-01-30

【言語別】複数行の標準入力→int型配列 の高速化(Tips)

最近paizaとかcodeiqがマイブームなのですが、POH1でI/Oで色々つまづいたので、言語別にそこそこ高速だと思われるプログラムを載せてみます。

標準入力1000000行の配列化&数値変換です。
(他のプログラムとの比較とかはないです。ごめんなさい。)

変数名とか色々まとまってません!

C,C++

#include <cstdio>
#include <vector>
#include <string>
#include <iostream>

static const int N = 1000000;
static char _buf[10*N];
int read_int(){
    static uint64_t c=0;

    int r=0;
    while( _buf[c] >= '0' && _buf[c] <= '9' ){
        r = r*10 + _buf[c++]-'0';
    }
    c++;
    return r;
}

int main(){
    /* C++ライクに書いてみた。がとても遅い。
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    std::vector<int> v(N);
    std::vector<int>::iterator it=v.begin();
    while(std::cin >> *it++);
    */

    fread(_buf,1,sizeof(_buf),stdin);

    int lines[N];
    for(int i=0;i<N;i++){
        lines[i] = read_int();
    }

    return 0;
}

C#

using System;

class Io {
    const int N=1000000;
    const int SIZE=N*10;
    static byte[] buf=new byte[SIZE];
    static int count=0;

    static int read_int(){
        int r=0;
        while(buf[count] >= '0' && buf[count] <= '9' ){
            r = r * 10 + buf[count++] - '0';
        }
        count++;
        return r;
    }

    public static void Main(string[] args){
        Console.OpenStandardInput().Read(buf,0,SIZE);

        int[] lines=new int[N];
        for(int i=0;i<N;i++){
            lines[i] = read_int();
        }
    }
}

Java

class Main {
    static final int N=1000000;
    static final int SIZE=N*10;
    static byte[] buf=new byte[SIZE];
    static int count=0;

    static int read_int(){
        int r=0;
        while(buf[count] >= '0' && buf[count] <= '9' ){
            r = r * 10 + buf[count++] - '0';
        }
        count++;
        return r;
    }

    public static void main(String[] args){
        try{
            System.in.read(buf,0,SIZE);
            int lines[] = new int[N];
            for(int i=0;i<N;i++){
                lines[i] = read_int();
            }
        }catch(Exception e){
            System.err.println(e);
        }
    }
}

Ruby

lines = $stdin.read.split(?\n).map(&:to_i)

Perl

=pod こちらは思ったより遅かった。。。
my $input = do { local $/; <> };
my @lines = split(/\n/,$input);
=cut

my @lines = ();
while(<>){
    push(@lines,int($_));
}

Python

import sys
_to_i=int
lines = map(_to_i,sys.stdin.read().splitlines())

PHP

<?php
/* これも思ったより遅かった。。。
$stdin = file_get_contents('php://stdin');
$lines = array_map(intval,preg_split('/\n/', $stdin));
*/
$lines = array();
while(($v = intval(fgets(STDIN)))){ 
    array_push($lines, $v);
}
?>

実行速度

最後に言語別の実行速度です。
(OS:Mac OS 10.9.1, CPU:Intel Core i5 2.9GHz)

言語 実行速度(timeコマンドのuser)
C,C++ 0.0022s
C# 0.046s
java 0.077s
perl 0.181s
ruby 0.257s
python 0.504s
php 0.590s

もっと速く...

なる方法を知っている方教えて下さい。お願いします。

44
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
44