gpt4 book ai didi

c - 使用链接在一起的多个箭头运算符 (->) 有什么缺点吗?

转载 作者:太空狗 更新时间:2023-10-29 17:03:07 25 4
gpt4 key购买 nike

假设我们有一些结构化数据,封装在另一个结构中,这样就可以形成一个循环链表。

typedef struct Data 
{
int x;
int y;
} Data;

typedef struct DataNode
{
struct DataNode *next;
struct Data *data;
} DataNode;

假设循环链表构造正确并且 *head 指向列表的成员,使用 -> 是否有任何缺点(性能或其他方面) > 链中的运算符,尤其是循环中的运算符?

DataNode * findPrevMatching(int x, int y)
{
// Chained arrow operators in a loop
while (!(head->next->data->x == x && head->next->data->y == y))
head = head->next;

return head;
}

如果我创建的局部变量没有链式箭头,会有什么不同吗?

DataNode * findPrevMatching(int x, int y)
{
DataNode *next = head->next;
Data *data = next->data;

while (!(data->x == x && data->y == y))
{
// Assign head->next to head
head = head->next;

// Assign each local variable, using the new head
next = head->next;
data = next->data;
}

return head;
}

最佳答案

在我的一条评论中,我指出我没有进行任何测量,只有测量才可能显示出任何有用的信息。我已经创建了一个 header 和代码的 4 个变体实现,如下所示:

节点.h

typedef struct Data 
{
int x;
int y;
} Data;

typedef struct DataNode
{
struct DataNode *next;
struct Data *data;
} DataNode;

extern DataNode *head;

extern DataNode *findPrevMatching(int x, int y);

节点1.c

#include "node.h"

DataNode * findPrevMatching(int x, int y)
{
// Chained arrow operators in a loop
while (!(head->next->data->x == x && head->next->data->y == y))
head = head->next;

return head;
}

节点2.c

#include "node.h"


DataNode * findPrevMatching(int x, int y)
{
DataNode *next = head->next;
Data *data = next->data;
int thisX = data->x;
int thisY = data->y;

while (!(thisX == x && thisY == y))
{
// Assign head->next to head
head = head->next;

// Assign each local variable, using the new head
next = head->next;
data = next->data;
thisX = data->x;
thisY = data->y;
}

return head;
}

node3.c

#include "node.h"


DataNode * findPrevMatching(int x, int y)
{
DataNode *next = head->next;
Data *data = next->data;

while (!(data->x == x && data->y == y))
{
head = head->next;
next = head->next;
data = next->data;
}

return head;
}

node4.c

#include "node.h"


DataNode * findPrevMatching(int x, int y)
{
DataNode *next = head->next;

while (!(next->data->x == x && next->data->y == y))
{
head = head->next;
next = head->next;
}

return head;
}

不同优化机制下的代码大小

使用此命令行重复编译代码,但用不同的值代替 -O0:

gcc -O0 -g -std=c11 -Wall -Wextra -Werror -c node1.c
gcc -O0 -g -std=c11 -Wall -Wextra -Werror -c node2.c
gcc -O0 -g -std=c11 -Wall -Wextra -Werror -c node3.c
gcc -O0 -g -std=c11 -Wall -Wextra -Werror -c node4.c

Mac OS X 10.11.6 上的 GCC 6.2.0

这些大小是由带有 GCC 6.2.0 的 Mac OS X 10.11.6 上的 size 命令报告的。

OFLAGS=-O0
__TEXT __DATA __OBJC others dec hex
176 0 0 1007 1183 49f node1.o
239 0 0 1226 1465 5b9 node2.o
208 0 0 1146 1354 54a node3.o
192 0 0 1061 1253 4e5 node4.o

OFLAGS=-O1
__TEXT __DATA __OBJC others dec hex
95 0 0 872 967 3c7 node1.o
125 0 0 1335 1460 5b4 node2.o
118 0 0 1182 1300 514 node3.o
114 0 0 993 1107 453 node4.o

OFLAGS=-O2
__TEXT __DATA __OBJC others dec hex
126 0 0 848 974 3ce node1.o
111 0 0 1410 1521 5f1 node2.o
121 0 0 1135 1256 4e8 node3.o
121 0 0 1005 1126 466 node4.o

OFLAGS=-O3
__TEXT __DATA __OBJC others dec hex
126 0 0 848 974 3ce node1.o
111 0 0 1410 1521 5f1 node2.o
128 0 0 1111 1239 4d7 node3.o
126 0 0 937 1063 427 node4.o

OFLAGS=-Os
__TEXT __DATA __OBJC others dec hex
101 0 0 848 949 3b5 node1.o
135 0 0 1293 1428 594 node2.o
112 0 0 1133 1245 4dd node3.o
107 0 0 1003 1110 456 node4.o

很明显,与任何优化相比,没有优化 (-O0) 最终得到的代码要大得多。在这段代码中,最小对象大小来自 node1.c 中的 -O1 优化代码。在-O2-O3处,node2.c中的代码最小。使用-Os-O1,第一个代码是最小的。

来自 XCode 8.0 的 Clang

来自 XCode 8.0 版的 clang 报告:

Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0

报告的尺寸是:

OFLAGS=-O0
__TEXT __DATA __OBJC others dec hex
189 0 0 925 1114 45a node1.o
245 0 0 1105 1350 546 node2.o
214 0 0 1023 1237 4d5 node3.o
198 0 0 970 1168 490 node4.o

OFLAGS=-O1
__TEXT __DATA __OBJC others dec hex
119 0 0 977 1096 448 node1.o
119 0 0 1071 1190 4a6 node2.o
119 0 0 1033 1152 480 node3.o
119 0 0 999 1118 45e node4.o

OFLAGS=-O2
__TEXT __DATA __OBJC others dec hex
104 0 0 973 1077 435 node1.o
104 0 0 1069 1173 495 node2.o
104 0 0 1031 1135 46f node3.o
104 0 0 997 1101 44d node4.o

OFLAGS=-O3
__TEXT __DATA __OBJC others dec hex
104 0 0 973 1077 435 node1.o
104 0 0 1069 1173 495 node2.o
104 0 0 1031 1135 46f node3.o
104 0 0 997 1101 44d node4.o

OFLAGS=-Os
__TEXT __DATA __OBJC others dec hex
104 0 0 973 1077 435 node1.o
104 0 0 1069 1173 495 node2.o
104 0 0 1031 1135 46f node3.o
104 0 0 997 1101 44d node4.o

结论

实验绝对是无可替代的!您可以查看汇编程序并决定您认为最好的代码。

关于c - 使用链接在一起的多个箭头运算符 (->) 有什么缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com