请选择 进入手机版 | 继续访问电脑版

C++编程

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1861|回复: 1

数据处理还是C语言效率最高

[复制链接]

1

主题

1

帖子

20

积分

菜鸟

Rank: 1

积分
20
发表于 2015-11-19 17:17:03 | 显示全部楼层 |阅读模式
本帖最后由 嬴政 于 2015-11-19 20:33 编辑

以前自己开发了perl小程序,用于处理intelhex文件到veriloghex文件的转换,采用读取数据到hash数组,然后hash排序输出,处理过程60多秒,今天用c重写了一个,采用自己开发的可有序插入的队列,甚是好用,处理起来才八九秒,顿时感觉还是指针功能超级强大
  
c部分代码~

  1. <ihex2vhex.c>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "queue.c"
  6. #define MAX 128

  7. char *substr(char *str_dst, char *str_src, int length);
  8. char *substr_noend(char *str_dst, char *str_src, int length);
  9. char *chmod(char *str);

  10. int main(void)
  11. {
  12. char file_name_src[MAX], file_name_dst[MAX];
  13. int file_src_valid, file_dst_valid;
  14. FILE *fp_src, *fp_dst, *fp_cfg;
  15. Queue line;
  16. Node * pn;
  17. Item item;

  18. char byte_count[3], address[5], record_type[3], data[33], address_tmp[10];
  19. unsigned long byte_count_i, address_i, record_type_i, base_address_i, address_tmp_i;      
  20. char *end;
  21. char content[45], sub_data[34];
  22. unsigned int i, num = 0;

  23. while(1)
  24. {
  25. do
  26. {
  27. printf("Please input the name of intelhex file:\n");
  28. //gets_s(file_name_src, MAX-1);
  29. //gets(file_name_src);
  30. fgets(file_name_src, MAX-1, stdin);
  31. chmod(file_name_src);
  32. if( (fp_src = fopen(file_name_src, "r")) == NULL  )
  33. {
  34. printf("Can't open %s\nPlease input the name of intelhex file correctly.\n", file_name_src);
  35. file_src_valid = 0;
  36. }
  37. else
  38. {
  39. strncpy(file_name_dst, file_name_src, MAX - 1);
  40. strncat(file_name_dst, ".out", MAX - 1);
  41. file_src_valid = 1;
  42. if( (fp_dst = fopen(file_name_dst, "w")) == NULL  )
  43. {
  44. printf("Can't open %s\nPlease make sure you have enough permissions to write this files.\n", file_name_dst);
  45. file_dst_valid = 0;
  46. }
  47. else
  48. {
  49. file_dst_valid = 1;
  50. }
  51. }
  52. }
  53. while(file_src_valid == 0 || file_dst_valid == 0);

  54. InitializeQueue(&line);
  55. item.hex[8] = '\n';//此值固定,初始化后不再处理
  56. item.hex[9] = '\0';//同上
  57. address_tmp[8] = '\0';//同上
  58. base_address_i = 0;

  59. while ( fgets(content, 45, fp_src) != NULL)  //此文件一般有几十万行,几十M大小
  60. {
  61. substr(byte_count, content + 1, 2);
  62. byte_count_i = strtoul(byte_count, &end, 16);
  63. substr(address, content + 3, 4);
  64. address_i = strtoul(address, &end, 16);
  65. substr(record_type, content + 7, 2);
  66. record_type_i = strtoul(record_type, &end, 16);
  67. substr(data, content + 9, byte_count_i * 2);

  68. address_tmp_i = (base_address_i + address_i)/4;

  69. if(record_type_i == 0)
  70. {
  71. for(i = 0; i < byte_count_i; i++)
  72. {
  73. substr_noend(item.hex + (3-i%4)*2, data + i*2, 2);//采用指针,每次只复制需要的2个字符,自动完成排序
  74. if(i%4 == 3)
  75. {
  76. item.addr = address_tmp_i;
  77. InsertSortQueue(item, &line);//将处理好的数据插入有序序列
  78. address_tmp_i++;
  79. }
  80. }
  81. }
  82. else if(record_type_i == 4)
  83. {
  84. strncat(data, "0000", 4);
  85. printf("%10d line Extended Linear Address Record, base_address:'h%s.\n", num, data);
  86. base_address_i = strtoul(data, &end, 16);
  87. }
  88. else if(record_type_i == 5)
  89. {
  90. printf("%10d line Start Linear Address Record.\n", num);
  91. }
  92. else if(record_type_i == 1)
  93. {
  94. printf("%10d line End Of File record.\n", num);
  95. printf("Start output veriloghex to file:\"%s\".\n", file_name_dst);
  96. pn= (&line)->front;
  97. while(pn != NULL)//有序输出
  98. {
  99. sprintf(address_tmp, "@%08x ", pn->item.addr);
  100. fputs(address_tmp, fp_dst);      
  101. fputs(pn->item.hex, fp_dst);
  102. pn = pn ->next;
  103. }
  104. fclose(fp_src);
  105. fclose(fp_dst);
  106. printf("\nThe intelhex to veriloghex successful!\n\n\n");
  107. }         
  108. else if(record_type_i == 2)
  109. {
  110. strncat(data, "0", 1);
  111. printf("%10d line Extended Segment Address Record, base_address:'h%s.\n", num, data);
  112. base_address_i = strtoul(data, &end, 16);
  113. }
  114. else if(record_type_i == 3)
  115. {
  116. printf("%10d line Start Segment Address Record.\n", num);
  117. }
  118. else
  119. {
  120. printf("ERROR: %10d line not support record type %s\n", num, record_type);
  121. printf("\nThe intelhex to veriloghex failure!\n\n\n");
  122. break;
  123. }
  124. num++;
  125. }
  126. }

  127. return 0;
  128. }

  129. char *substr(char *str_dst, char *str_src, int length)
  130. {
  131. register int m=0;
  132. while(m < length && str_src[m] != '\n' && str_src[m] != '\0')//应用时会先遇到m<length不成立较多直接结束判断,后续'\n''\0'也是有序的选择
  133. {
  134. str_dst[m] = str_src[m];
  135. m++;
  136. }
  137. str_dst[m] = '\0';
  138. return str_dst;
  139. }

  140. char *substr_noend(char *str_dst, char *str_src, int length)
  141. {
  142. register int m=0;
  143. while(m < length && str_src[m] != '\n' && str_src[m] != '\0')
  144. {
  145. str_dst[m] = str_src[m];
  146. m++;
  147. }
  148. return str_dst;
  149. }

  150. char *chmod(char *str)
  151. {
  152. register int m=0;
  153. while(str[m] != '\n' && str[m] != '\0')
  154. {
  155. m++;
  156. }
  157. str[m] = '\0';
  158. return str;
  159. }

  160. <queue.c>核心函数
  161. bool InsertSortQueue(Item item, Queue * pq)
  162. {
  163. Node * pnew, * psearch;

  164. //if(QueueIsFull(pq))
  165. //        return false;
  166. pnew = (Node *)malloc(sizeof(Node));
  167. //if(pnew == NULL)
  168. //{
  169. //        fprintf(stderr, "Unable to allocate memory!\n");
  170. //        exit(1);
  171. //}

  172. CopyToNode(item, pnew);
  173. pnew->next = NULL;

  174. if(pq->items == 0)
  175. {
  176. pq->front = pnew;
  177. pq->rear = pnew;
  178. }
  179. else
  180. {
  181. psearch = pq->rear;//此处考虑后来的数据地址一般都是增长的(但也有乱序情况),选择从队列尾部向前搜索的方式,能较早找到有序插入的位置
  182. while(pnew->item.addr < psearch->item.addr)
  183. psearch = psearch->pre;
  184. if(psearch->next == NULL)
  185. {
  186. psearch->next = pnew;      
  187. pnew->pre = psearch;
  188. pq->rear = pnew;
  189. }
  190. else
  191. {
  192. pnew->pre = psearch;
  193. pnew->next = psearch->next;
  194. psearch->next->pre = pnew;
  195. psearch->next = pnew;      
  196. }
  197. }
  198. pq->items++;

  199. return true;
  200. }
复制代码


回复

使用道具 举报

8

主题

31

帖子

323

积分

版主

Rank: 7Rank: 7Rank: 7

积分
323
QQ
发表于 2015-11-19 20:34:41 | 显示全部楼层
C语言是运行高效,但开发进度慢,各种东西要懂,才能写程序
VC纵横、磐实编程网
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|C++编程  

GMT+8, 2019-8-19 07:17 , Processed in 0.093750 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表