用C语言写一个每秒向test.txt文件中写1M随机数据的程序
时间: 2023-02-19 20:15:08 浏览: 180
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w");
if (fp == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i;
time_t start_time = time(NULL);
while (time(NULL) - start_time < 1)
{
for (i = 0; i < 1000000; i++)
{
int x = rand();
fprintf(fp, "%d\n", x);
}
}
fclose(fp);
return 0;
}
相关问题
请用C语言帮我写一个关于SKF_Encrypt()函数的测试用例,测试其准确性和TPS性能
好的,以下是一个简单的测试用例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "skf.h"
#define TEST_DATA_LEN 1024
int main()
{
// 初始化加密机
ULONG rv;
HAPPLICATION hApp;
rv = SKF_ConnectDev("ACOS5-64", 0, 0, "", &hApp);
if (rv != SAR_OK) {
printf("Failed to connect to device. Error code: %08X\n", rv);
return -1;
}
// 生成测试数据
BYTE *plainData = (BYTE *)malloc(TEST_DATA_LEN);
BYTE *cipherData = (BYTE *)malloc(TEST_DATA_LEN);
BYTE *decryptedData = (BYTE *)malloc(TEST_DATA_LEN);
if (plainData == NULL || cipherData == NULL || decryptedData == NULL) {
printf("Failed to allocate memory.\n");
return -1;
}
srand(time(NULL));
for (int i = 0; i < TEST_DATA_LEN; i++) {
plainData[i] = rand() % 256;
}
// 测试准确性
ULONG ulDataLen = TEST_DATA_LEN;
rv = SKF_Encrypt(hApp, plainData, ulDataLen, cipherData, &ulDataLen);
if (rv != SAR_OK) {
printf("Failed to encrypt data. Error code: %08X\n", rv);
return -1;
}
rv = SKF_Decrypt(hApp, cipherData, ulDataLen, decryptedData, &ulDataLen);
if (rv != SAR_OK) {
printf("Failed to decrypt data. Error code: %08X\n", rv);
return -1;
}
if (memcmp(plainData, decryptedData, TEST_DATA_LEN) != 0) {
printf("Decrypted data is different from plain data.\n");
return -1;
}
printf("Accuracy test passed.\n");
// 测试TPS性能
clock_t start = clock();
for (int i = 0; i < 100; i++) {
rv = SKF_Encrypt(hApp, plainData, ulDataLen, cipherData, &ulDataLen);
if (rv != SAR_OK) {
printf("Failed to encrypt data. Error code: %08X\n", rv);
return -1;
}
}
clock_t end = clock();
double seconds = (double)(end - start) / CLOCKS_PER_SEC;
printf("TPS: %.2lf\n", 100 / seconds);
// 释放资源
free(plainData);
free(cipherData);
free(decryptedData);
SKF_DisconnectDev(hApp);
return 0;
}
```
这个测试用例首先连接加密机,并生成一个长度为1024的随机数据作为测试数据。然后它执行了两个测试:
1. 准确性测试:使用SKF_Encrypt函数对测试数据进行加密,然后再使用SKF_Decrypt函数对密文进行解密,最后比较解密后的数据和原始数据是否相同。如果测试通过,则表示SKF_Encrypt函数的准确性正常。
2. TPS性能测试:使用SKF_Encrypt函数对测试数据进行100次加密操作,并计算总共用时。然后将100除以总共用时,得到每秒加密次数。如果每秒加密次数较高,则表示SKF_Encrypt函数的TPS性能较好。
注意:这个测试用例是简单的示范,实际情况下可能需要更复杂的测试用例来覆盖更多的场景和数据。
阅读全文