Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created June 18, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaityo256/fab06231d83c87c62fba to your computer and use it in GitHub Desktop.
Save kaityo256/fab06231d83c87c62fba to your computer and use it in GitHub Desktop.
std::fill_nをmemcpyで書いてみた ref: http://qiita.com/kaityo256/items/22c705fbfff164dd4ef6
$ icpc --version
icpc (ICC) 12.1.4 20120410
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.
$ g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)
$ g++ -O0 fill.cc;./a.out
N=268435455
fill_n 0.479488
fillfore 0.617751
myfill 0.125472
$ g++ -O3 fill.cc;./a.out
N=268435455
fill_n 0.115933
fillfore 0.115964
myfill 0.130052
$ icpc -O0 fill.cc;./a.out
N=268435455
fill_n 0.490290
fillfore 0.596837
myfill 0.126500
$ icpc -O3 fill.cc;./a.out
N=268435455
fill_n 0.055505
fillfore 0.055506
myfill 0.141159
$ icpc -O3 -opt-streaming-stores never fill.cc;./a.out # 絶対使うな
N=268435455
fill_n 0.115863
fillfore 0.115882
myfill 0.137723
$ icpc -O3 -opt-streaming-stores always fill.cc;./a.out # 絶対使え
N=268435455
fill_n 0.056443
fillfore 0.056552
myfill 0.141629
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <sys/time.h>
#include <stdint.h>
const int S = 28;
const int N = (1<<S)-1;
int a[N];
double mytime(void){
timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec + (double)tv.tv_usec*1e-6;
}
void
myfill(int *a, const int N, const int value){
a[0] = value;
for(int i=0;i<S;i++){
const int s = 1 << i;
memcpy((void*)(&a[s]),(void*)(&a[0]),s*sizeof(int));
}
}
void
fillfor(int *a, const int N, const int value){
for(int i=0;i<N;i++){
a[i] = value;
}
}
#define measure(func,name) {\
double s1 = mytime();\
func(a,N,12345);\
double s2 = mytime();\
printf("%s %f\n",name,s2-s1);};
int
main(void){
printf("N=%d\n",N);
std::fill_n(a,N,0); //最初に触っておく
measure(std::fill_n,"fill_n ");
measure(fillfor ,"fillfore");
measure(myfill ,"myfill ");
for(int i=0;i<N;i++){ //一応チェック
if(a[i] != 12345){
printf("Error\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment