zoukankan      html  css  js  c++  java
  • CUDA中的计时函数

    一、clock函数计时

    在C和C++中有clock计时函数,由于CUDA是包含C的,所以在CUDA中我们也同样可以使用这个函数。

    clock函数的定义:clock函数测的是在程序中从程序开始到调用clock函数之间在CPU上所经过的时钟数(CLOCKS)。

    clock函数的介绍:

    在C与C++的头文件time.h与ctime中的库函数clock()可以测试函数运行时间

    1.clock()返回类型为clock_t类型

    2.clock_t实际为long类型,typedef long clock_t

    3.clock()函数,返回从开启这个程序进程到程序中调用clock()函数时之间的CPU时钟计时单元(clock tick)数,返回单位是毫秒

    4.可以用常量CLOCKS_PER_SEC,这个常量表示每一秒(per second) 有多少个时钟计时单元

    函数用法:

    #include<time.h>  // 头文件
    
    clock_t start,end;     //  clock_t 数据类型
    
    start = clock();   //  需要计时的部分开始
    
    end = clock();    //需要计时的部分结束
    
    float time = (float)(end-start)/CLOCKS_PER_SEC;
    
    cout<<time<<endl;

    二、CUDA中的事件计时(Event)

    用法:

    float time_elapsed = 0;
    
    cudaEvent_t start,stop;
    
    cudaEventCreate(&start);       //创建Event
    
    cudaEventCreate(&stop);
    
    cudaEventRecord( start,0);    //记录当前时间
    
    xxxxxx           //  xxxxxx指的是需要在GPU上运行的函数  如核函数kernel
    
    cudaEventRecord( stop,0);    //记录当前时间
    
    cudaEventSynchronize(start);    //Waits for an event to complete.
    
    cudaEventSynchronize(stop);    //Waits for an event to complete.Record之前的任务
    
    cudaEventElapsedTime(&time_elapsed,start,stop);    //计算时间差
    
    cudaEventDestroy(start);    //destory the event
    
    cudaEventDestroy(stop);
    
    cout<<time_elapsed<<endl;
  • 相关阅读:
    Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
    .net extjs 封装
    ext direct spring
    install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
    redis cookbook
    aptana eclipse plugin install on sts
    ubuntu open folderpath on terminal
    ubuntu install pae for the 32bit system 4g limited issue
    EXT Designer 正式版延长使用脚本
    用 Vagrant 快速建立開發環境
  • 原文地址:https://www.cnblogs.com/Leozi/p/13281213.html
Copyright © 2011-2022 走看看