showMem.c setMem.c 及其改进

时间:2023-03-10 03:36:53
showMem.c  setMem.c 及其改进
#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 优化调整