로그인

검색

MoA
조회 수 3054 추천 수 0 댓글 2
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄
http://gallerc.tistory.com/263
 
memset과 for반복 초기화의 속도 차이를 알아보기 위한 간단한 실험을 해보았습니다. 실험은 E5200 CPU/DDR2(SamSung) 2GB RAM에서 진행되었으며 결과를 정확하게 구하기 위해 1000번 반복하여 실험하였음을 밝힙니다.

#include <stdio.h> 
#include <memory.h> 
#include <time.h> 
#define KB 65536 // 256KB 
#define MB 262144 // 1MB 

int main(){ 

int *arr; 
clock_t t; 
double time; 
int i, j; 
time = 0; 

for(i=0;i<1000;i++){ 
arr = new int[KB]; 
t = clock(); 
memset(arr, 100, KB*4); 
time+=clock()-t; 
delete []arr; 


printf("memset으로 256KB 메모리를 100으로 초기화 : %.3fms(밀리초)n", time/1000); 

time = 0; 
for(i=0;i<1000;i++){ 
arr = new int[KB]; 
t = clock(); 
for(j = 0 ; j<KB;j++) 
arr[i] = 100; 
time+=clock()-t; 
delete []arr; 


printf("65536번 반복해서 256KB 메모리를 100으로 초기화 : %.3fms(밀리초)n", time/1000); 

time=0; 
for(i=0;i<1000;i++){ 
arr = new int[MB]; 
t = clock(); 
memset(arr, 100, MB*4); 
time+=clock()-t; 
delete []arr; 


printf("memset으로 1MB 메모리를 100으로 초기화 : %.3fms(밀리초)n", time/1000); 

time = 0; 
for(i=0;i<1000;i++){ 
arr = new int[MB]; 
t = clock(); 
for(j = 0 ; j< MB;j++) 
arr[j] = 100; 
time+=clock()-t; 
delete []arr; 


printf("262144번 반복해서 1MB 메모리를 100으로 초기화 : %.3fms(밀리초)n", time/1000); 
}


?
  • ?
    지나가던사람 2013.11.01 22:29
    2/4 안에 인덱스가 j여야 하지 않나요?
    그리고 memset은 1바이트씩 하는거라 4*kb 하면 공평한 측정이 아닌 거 같네요
    int 를 char 로 바꾸고 횟수를 똑같이해야 맞는거 같네요
  • profile
    MoA 2013.11.02 15:38

    퍼온 자료인데 세부 내용을 확인 못했었네요.

    1. 2번째 안의 인덱스는 j가 맞습니다.

    2. 첫번째와 두번째 배열에 입력되는 값이 다르겠네요. 첫 번째는 0x64646464가 되는 것이니...
    memset(arr, 0, 4*KB) 한 것과 arr[j] = 0 한 것을 비교하거나 말씀하신 방법대로 하는게 정확할 것 같아요.

    그렇지만 전자와 후자의 방법이 다른 의미를 가지겠네요.

    int형 변수에 4N byte를 할당한 경우에는 for 루프를 N번 돌겠지만 char형 변수에 4N byte를 할당한 경우에는 for 루프를 4N번 도니까요.


  1. Programming 게시판 관련

    Date2014.11.01 CategoryTool/etc ByMoA Views15455
    read more
  2. How LLMs Work ? Explained in 9 Steps — Transformer Architecture

    Date2024.04.11 CategoryLLM ByOBG Views2459
    Read More
  3. How to send dynamic charts with a Slack bot

    Date2022.05.31 CategoryWeb ByOBG Views2259
    Read More
  4. How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04

    Date2023.01.17 CategoryTool/etc ByOBG Views2664
    Read More
  5. How to stop programmers to copy the code from GitHub when they leave the company?

    Date2024.01.02 CategoryTool/etc ByOBG Views2788
    Read More
  6. HuggingFace 공동창업자가 추천하는 AI 분야 입문 서적

    Date2024.05.24 CategoryTool/etc ByOBG Views2606
    Read More
  7. HWND와 HINSTANCE

    Date2013.07.28 CategoryAPI/MFC ByMoA Views3607
    Read More
  8. InvalidateRect 함수, flag성 메세지

    Date2013.07.25 CategoryAPI/MFC ByMoA Views2616
    Read More
  9. IT 세미나 유튜브 동영상

    Date2012.09.10 CategorySite ByNaya Views2689
    Read More
  10. Keras를 활용한 주식 가격 예측

    Date2022.09.02 CategoryDeeplearning ByOBG Views2463
    Read More
  11. Legacy MFC 어플리케이션을 MFC feature pack으로 포팅

    Date2013.07.30 CategoryAPI/MFC ByMoA Views2672
    Read More
  12. LLaMA: INT8 edition

    Date2023.03.09 CategoryDeeplearning ByOBG Views2645
    Read More
  13. llama3 implemented from scratch

    Date2024.05.24 CategoryLLM ByOBG Views1618
    Read More
  14. LLM 출력 속도 24배 높여주는 라이브러리 등장했다

    Date2023.06.30 CategoryLLM ByOBG Views2552
    Read More
  15. logcat 사용법

    Date2013.05.28 CategoryJAVA/Android ByMoA Views5786
    Read More
  16. LSTM-AE를 이용한 시퀀스 데이터 이상 탐지

    Date2023.08.14 CategoryDeeplearning ByOBG Views2243
    Read More
  17. Machine Learning for Video Games

    Date2015.07.27 CategoryTool/etc ByMoA Views3419
    Read More
  18. Math Library

    Date2012.08.02 CategoryLibrary ByNaya Views3018
    Read More
  19. memset vs for 초기화. 속도 차이가 얼마나 날까?

    Date2013.07.28 CategoryC/C++ ByMoA Views3054
    Read More
  20. MFC Feature Pack: An Introduction

    Date2012.02.08 CategoryAPI/MFC By너울 Views4068
    Read More
  21. MFC TIP

    Date2013.07.28 CategoryAPI/MFC ByMoA Views2885
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 15 Next
/ 15