#ifndef MEMUTIL_H_INCLUDED
#define MEMUTIL_H_INCLUDED // Show memory
void showMem(void *, unsigned); // Setup memory
int setMem(void *, const char *); #endif // MEMUTIL_H_INCLUDED

  

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Display memory
void showMem(void *p, unsigned size)
{
char *buf = 0;
int prs = 0;
unsigned i; buf = (char *)malloc(size * 9);
printf("Show %p, count %u bits.\n", p, size * 8);
for (i = 0; i < size; i++)
{
char ch = ((char *)p)[i]; // Get char p[i]
int j; for (j = 0; j < 8; j++) // p[i] to 8 bit unsigned int
{
unsigned tmp = 0; tmp = ch >> (8 - j - 1) & 1;
sprintf(&buf[prs], "%u", tmp % 2);
prs = strlen(buf);
}
sprintf(&buf[prs], " ");
prs++;
}
buf[prs - 1] = '\0';
puts(buf);
}

  

#include <stdio.h>
#include <string.h>
#include <stdlib.h> #include "MemUtil.h" // Setup memory
// Return:
// 0 -Setting succeed
// !0 -Setting failed
int setMem(void *p, const char *c)
{
int le;
char *str = 0; // 去除空格的副本
int i;
int j;
char ch; le = strlen(c);
i = le - 1;
str = (char *)malloc(le * 8 / 9 + 2);
str[0] = '\0'; // 去除空格
i = 0;
while ((ch = *(char *)c++) != '\0')
{
if (ch == ' ')
continue;
if (ch == '1' || ch == '0')
{
str[i++] = ch;
str[i] = '\0';
} else {
printf("错误, 未知的字符: %c.", ch);
return !0;
}
}
if ((le = strlen(str)) % 8 != 0)
{
printf("拒绝执行, 长度错误: %d.\n", le % 8);
return !0;
} i = 0;
j = le / 8;
for (; i < j; i++)
{
char tmp = 0;
int k = 0; for (; k < 8; k++)
{
tmp |= (str[i * 8 + k] ^ 0x30) << (7 - k);
}
((char *)p)[i] = tmp;
}
return 0;
}

  上面这些代码看似可以工作, 然而!!!!!!!!!!!

  就在某一天我准备使用它打造一个二进制文件工具的时候, 发生了爆炸!!!!!!! showMem 处理 200 KB居然耗时 80 s !!!!!!!!!!!!!!!!!!!! 当然其中 puts 占用了绝大部分"功劳", 试着优化一下

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> char *showMem(void *p, unsigned size)
{
char *buf = ;
int prs = ;
unsigned i = ;
unsigned j = ;
char ch = '\0';
unsigned tmp = ; if ((buf = (char *)malloc(size * )) == )
{
fprintf(stderr, "Execute failed, PC have not memory.\n");
return ;
} printf("Show %p, count %u bits.\n", p, size * );
for (i = ; i < size; i++)
{
ch = ((char *)p)[i]; // Get char p[i] for (j = ; j < ; j++) // p[i] to 8 bit unsigned int
{
tmp = ch >> ( - j) & ;
buf[prs++] = (tmp == ? 0x30 : 0x31);
}
buf[prs++] = ' ';
}
buf[prs - ] = '\0';
puts(buf);
return buf;
}

showMem.c 性能40+倍提升( -_-! )

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include "MemUtil.h" // Setting memory
// Return:
// 设置完成的字节数
// -1 代表错误
int setMem(void *p, const char *c)
{
int le = ;
char *str = ; // 去除空格的副本
int i = ;
int j = ;
char ch = ; le = strlen(c);
str = (char *)malloc(le); // 去除空格和换行
while ((ch = *(char *)c++) != '\0')
{
if (ch == ' ' || ch == '\n')
continue;
if (ch == 0x31 || ch == 0x30)
{
str[i++] = ch;
} else {
printf("Oops, dead character %c.", ch);
return -;
}
}
str[i] = '\0'; if ((le = strlen(str)) % != )
{
printf("You setting data is fucking, 错误的余数 %d.\n", le % );
return -;
} i = ;
j = le / ;
for (; i < j; i++)
{
char tmp = ;
int k = ; for (; k < ; k++)
{
tmp |= (str[i * + k] ^ 0x30) << ( - k);
}
((char *)p)[i] = tmp;
}
return j;
}

setMem.c 优化调整

最新文章

  1. ubuntu下安装lrzsz
  2. sruts2:单个文件上传,多个文件上传(属性驱动)
  3. Extjs中renderer:function函数用法
  4. 紧张:飞测独家のJmeter秘籍,限量发放
  5. 一些时间的概念与区分(UTC、GMT、LT、TAI等)
  6. 最受欢迎的5款PHP框架记录,我居然一个不知道。。。
  7. eclipse 启动tomcat报Spring错误 Error creating bean with name &#39;serviceOrderBiz&#39;: Injection of autowired dependencies failed
  8. nyist0j 35 表达式求值
  9. linux+nginx+mysql+php高性能服务器搭建
  10. 基于 WebSocket 构建跨浏览器的实时应用
  11. VS根据数据库生成实体类
  12. 《javascript设计模式与开发实践》--- (单一职责原则)
  13. [Android][Framework] 添加系统服务
  14. json-lib转化java对象,是否转化为null的属性
  15. Hibernate的实体类中为什么要继承Serializable?
  16. 问题:虚拟环境名字相同遇到bug,please specify a different sdk name?
  17. fastdfs(https://www.jianshu.com/p/1c71ae024e5e)
  18. Wince/VC高效PNG贴图,自定义Alpha算法
  19. feed流拉取,读扩散,究竟是啥?
  20. Linux学习4-信号

热门文章

  1. 【POJ1704】Georgia and Bob(博弈论)
  2. 136.137.260. Single Number &amp;&amp; 位运算
  3. p1184高手之在一起
  4. java thread 线程40个问题汇总
  5. Linux中OBS在Wayland环境下黑屏只显示鼠标的应对措施
  6. C# Winform ListView控件
  7. cf 990G - GCD Counting
  8. 洛谷P1080 国王游戏
  9. 洛谷P2414 阿狸的打字机
  10. 【洛谷P3455】ZAP-Queries