C

获得数组长度

1
2
int a[] = {1, 2, 3, 4};
l = sizeof(a)/sizeof(int);

printf输出格式

%d 有符号十进制整数(int)
%ld, %Ld 长整形数据(long)
%i 有符号十进制数,和%d一样
%u 无符号十进制整数(unsigned int)
%lu, %Lu 无符号十进制长整形数据(unsigned long)
%f 单精度浮点数(float)
%c 单字符(char)
%o 无符号八进制整数
%x(%X) 十六进制无符号整数

c类型字符串数组

  1. 导包
1
#include <string.h>
  1. 完整字符串复制
1
strcpy(des, src);
  1. 部分字符串复制
1
strncpy(des, src+n, len);
  1. 结束符
1
sub[len] = '\0';
  1. new字符串数组
1
char *str = new char[100];
  1. delete字符串数组
1
delete []str;

生成随机数

1
2
3
4
5
#include <time.h>
#include <stdlib.h>

srand(time(NULL)); // Initialization, should only be called once.
int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.

计算函数运行时间

计算实际运行时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<sys/time.h>
#include<unistd.h>


//struct timeval
//{
// long tv_sec;//秒域
// long tv_usec;//微秒域
//}
//int getimeofday(struct timeval* tv,NULL);

int main()
{

struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
sleep(2);
gettimeofday(&tv2, NULL);

printf ("Total time = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
return 0;
}

计算cpu运行时间

clock()返回的是OS花了多少时间运行当前的进程。

1
2
3
4
5
6
7
8
9
10
11
#include<ctime>

int main()
{

clock_t start, end;
start = clock();
time.sleep(10);
end = clock()
print("%f", (double)(end-start)/CLOCK_PER_SEC);
}

参考文献

1.https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c
2.https://blog.csdn.net/zhangwei_zone/article/details/11219757
3.https://stackoverflow.com/questions/5248915/execution-time-of-c-program