LoginSignup
0
0

More than 3 years have passed since last update.

GolangとC++を比較してみた1: for文

Last updated at Posted at 2019-09-21

なにこれ?

C++を勉強していく過程で、私にとって既知であるGolangと、その実行速度を比較してみようという企画の第1弾です。
早速、ソースコードと実行結果の抜粋を見てみましょう。

Golangのソースコード

main.go
package main

import (
   "fmt"
   "time"
)

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

   for i:=1; i<=100000; i++ {
     if(i % 5000 == 0) {
       fmt.Println(i)
     }
   }

   end := time.Now()

   fmt.Printf("time %f[ms]\n", (end.Sub(start)).Seconds()*1000.0)
}

C++のソースコード

main.cpp
#include <iostream>
#include <time.h>

using namespace std;

int main() {
     clock_t start = clock();

     for(int i=1; i<=100000; i++){
       if(i % 5000 == 0){
         cout << i << endl;
       }
     }

     clock_t end = clock();

     const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
     printf("time %lf[ms]\n", time);

     return 0;
 }

実行結果比較


# Golang
time 0.275946[ms]

#C++
time 0.869000[ms]

終わりに

意外でしょうか?妥当でしょうか?
私自身としては、思ったより差がついたと思いました。
今回は単にfor文を回しただけですので、次回はもっと複雑な処理を実行させて比較したいと思います。

0
0
2

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
0
0