统计程序运行时间的错误示范

Motivation:对程序实现测速。

初始想法是使用两点计时的方式,在程序运行语句之前设置时间点,程序运行语句之后设置时间点,然后计算二者的差值。

考虑到程序执行时间的不确定性,设置程序执行1K次。

代码如下:

  //Time
  clock_t first, second;

  // Run
  printf("rknn_run\n");

  double statistics = 0;
  double num = 0;
  for (int i = 0; i < 1000; i++) {
    first = clock();
    ret = rknn_run(ctx, nullptr); #程序运行语句
    second = clock();
    num = (double)(second-first)/CLOCKS_PER_SEC;
    printf("%d  %f seconds\n", i, num);
    statistics += num;
  }
  printf("Statistics: %f \n", statistics/1000);  

发现实际上记录的时间并不正确。后面尝试寻找原因。

可以采用最原始的方法,程序循环执行一个比较大的次数,统计循环执行的总时间,然后求平均。

参考链接:

  1. #include 库函数详解_大米粒ing的博客-CSDN博客_#include time.h 有人总结成这么几句,的确是经典,自己好好编程试试效果吧,两个类型:time_t:表示距离 UTC 时间 1970-01-01 00:00:00 的秒数。也叫做日历时。struct tm:通常用于存储本地时。几个函数:time:获取当前系统时间(UTC时间)的time_t值。ctime:将time_t值转换为表示本地时间的字符串。gmttime:将thttps://blog.csdn.net/chenyiming_1990/article/details/8682552
  2. null头文件#include <time.h>clock_t start, end;//定义/*开始时间*/start = clock();......end = clock();/*结束时间*/得到运行时间double time = (double)(end - start)/CLOCKS_PER_SEC;...https://blog.csdn.net/dsbss/article/details/120937234
  3. #include <time.h>_迷糊小旋风的博客-CSDN博客_#include #include <time.h>clock_t clock(void);这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数。clock_t是用来保存时间的长整形数据类型。还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个clock tick,一般取值为1000。核心代码: double TheTimes; printf("做%ld次空循环需要的时间为",i)https://blog.csdn.net/weixin_49828466/article/details/117333648
### 回答1: 以下是一个简单的 Python 爬虫程序示范: ```python import requests from bs4 import BeautifulSoup url = 'https://www.example.com' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') for link in soup.find_all('a'): print(link.get('href')) ``` 这个程序使用 requests 库发送 HTTP 请求,然后使用 BeautifulSoup 库解析 HTML 页面,最后打印出页面中所有链接的 URL。 ### 回答2: 以下是一个简单的爬虫程序示范: ```python import requests from bs4 import BeautifulSoup # 首先发送 HTTP 请求获取网页内容 response = requests.get("https://example.com") # 使用 BeautifulSoup 解析网页内容 soup = BeautifulSoup(response.text, "html.parser") # 查找网页中的特定元素 titles = soup.find_all("h1") # 查找所有的 h1 标签 # 打印标题 for title in titles: print(title.text) # 查找网页中的链接 links = soup.find_all("a") # 查找所有的链接 # 打印链接 for link in links: print(link.get("href")) # 将爬到的数据保存到文件 with open("output.txt", "w", encoding="utf-8") as file: for title in titles: file.write(title.text + "\n") ``` 以上是一个基本的爬虫程序示范,它使用了 Python 的 requests 库来发送 HTTP 请求,使用 BeautifulSoup 库来解析网页内容,并使用一些简单的操作来提取所需的数据。示范中的程序发送了一个 GET 请求到示例网页(https://example.com),然后使用 BeautifulSoup 解析网页内容并找到其中的 h1 标签和链接,并将它们打印出来和保存到文件中。通过这个简单示范,您可以了解到使用爬虫程序获取网页内容和提取数据的基本步骤和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值