小编nin*_*alj的帖子

Code Golf:绘制ascii艺术明星

由于本周没有人发布过代码高尔夫挑战,我会试一试.我这样做是为了在漫长的编译周期中除了玩剑之外你还能做些什么.

挑战:

绘制ASCII艺术星,在标准输入上给出三个数字(尖峰的数量,星的类型(通过连接n个顶点的顶点绘制星形)和星的直径).例子:

Input:                        Input:                       Input:

5 2 20                        7 2 20                       7 3 20

Output:                       Output:                      Output:

             x                        x                        x
            xx                        xx                       x
            xx                       x  xx   xx                xx      x
           x x                       x   xxxx x                xx     xx
  xx      x  x                      xxxxx  x  x                x x  xxx
   xxxx   x  x                   xxxx       x x                x x x  x
   x   xxx   x                 xx  x         xx         xxx    x  x  x
    x   x xxxx                 x   x          xx         x xxxxx xx  x
     x  x    xxx                x …
Run Code Online (Sandbox Code Playgroud)

language-agnostic code-golf rosetta-stone

25
推荐指数
2
解决办法
2353
查看次数

strftime性能与snprintf

我遇到了一个有趣的性能难题,但在我开始深入研究glibc并在左右中心输入错误之前,我只想获得可能存在的任何洞察力.

我有一个代码,在其中一个函数中执行此操作:

gettimeofday( &tv, 0);
localtime_r( &tv.tv_sec, &local_tm );
char result[25];
strftime( result, 24, "%Y-%m-%d %H:%M:%S", &local_tm);
Run Code Online (Sandbox Code Playgroud)

其余代码与此问题无关.当我用这个替换它:

gettimeofday( &tv, 0);
localtime_r( &tv.tv_sec, &local_tm );
char result[25];
snprintf(result, sizeof(result), "%04d-%02d-%02d %02d:%02d:%02d",
         local_tm.tm_year+1900, local_tm.tm_mon+1,
         local_tm.tm_mday, local_tm.tm_hour, local_tm.tm_min,
         local_tm.tm_sec);
Run Code Online (Sandbox Code Playgroud)

平均而言,我的性能提升了20%.

有没有人碰到这个?这个操作系统特定吗?

c c++ performance printf strftime

7
推荐指数
1
解决办法
1709
查看次数

thrd_busy和mtx_lock()/ mtx_timedlock()

我有关于C1x互斥量的以下问题(§7.25.4):

在哪些情况下可以mtx_lock()返回thrd_busy而不是阻止?在哪些情况下可以mtx_timedlock()返回thrd_busy

注意,thrd_busy在§7.25.15中定义为" 当测试和返回函数请求的资源已被使用时 ".

我希望thrd_busy只返回mtx_trylock(),或者最多也只能通过mtx_lock()一个mtx_trymtx_try | mtx_recursive互斥锁调用,但绝对不是来自mtx_timedlock(),这需要一个支持超时的互斥锁,即一个mtx_timedmtx_timed | mtx_recursive互斥锁.

这只是草案中的公正和疏忽吗?或者我错过了什么?

c multithreading mutex c11

4
推荐指数
1
解决办法
1709
查看次数

具有相同代码的程序的运行结果在不同平台下是不同的

我编写这样的代码来帮助我理解realloc()函数的用法.也许在以下代码中存在一些问题.

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 
 6 void dynamic_memory_management_func()
 7 {
 8     printf("------------dynamic_memory_management_func------------\n");
 9 
10     char *str1 = (char *)malloc(16);
11     memset(str1, 0, 16);
12     strcpy(str1, "0123456789AB");
13     char *str2 = realloc(str1, 8);
14     
15     printf("str1 value: %p [%s]\n", str1, str1);
16     printf("str2 value: %p [%s]\n", str2, str2);
17     
18     free(str2);
19     str2 = NULL;
20     
21     char *str3 = (char *)malloc(16);
22     memset(str3, 0, 16);
23     strcpy(str3, "0123456789AB");
24     char *str4 …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
58
查看次数