Пример #1
0
// 2. 분할 정복법으로 푸는 방법
//    시간 복잡도가 O(logn)
int fastsum(int n)
{
	if (n == 1)
		return 1;
	// 홀수
	if (n % 2)
		return fastsum(n - 1) + n;
	// 짝수
	return 2 * fastsum(n / 2) + n / 2 * n / 2;
}
Пример #2
0
int main()
{
	//start_timer();
	//printf("%d\n", sum(100000));
	//end_timer();

	start_timer();
	printf("%d\n", fastsum(100000));
	end_timer();
}
void demo(uint32_t N) {
  printf("N = %d\n", N);
  uint32_t * z = malloc(N * sizeof(uint32_t));
  for(uint32_t i = 0 ; i < N; ++i) z[i] = rand(); // some rand. number
  uint32_t nmbr = 500;
  uint32_t * accesses = malloc(nmbr * sizeof(uint32_t));
  for(uint32_t i = 0 ; i < nmbr; ++i) accesses[i] = rand(); // some rand. number
  uint32_t expected1 = modsum(z,N,accesses,nmbr);
  uint32_t expected2 = fastsum(z,N,accesses,nmbr);
  BEST_TIME(modsum(z,N,accesses,nmbr), expected1, 1000, nmbr);
  BEST_TIME(fastsum(z,N,accesses,nmbr), expected2, 1000, nmbr);
#ifdef __AVX2__
  uint32_t expected3 = vectorsum(z,N,accesses,nmbr);
  if(N % 4 == 0) BEST_TIME(vectorsum(z,N,accesses,nmbr), expected3, 1000, nmbr);
  uint32_t expected4 = maskedvectorsum(z,N,accesses,nmbr);
  if(N % 8 == 0) BEST_TIME(maskedvectorsum(z,N,accesses,nmbr), expected4, 1000, nmbr);
#endif
  free(z);
  free(accesses);
}