Compile / runtime differences for using import statements
Salih Dincer
salihdb at hotmail.com
Fri Apr 28 23:25:54 UTC 2023
On Thursday, 20 April 2023 at 23:04:14 UTC, Selim Ozel wrote:
> I've been wondering if there is any difference between
> importing a function/component from a module as stand-alone or
> importing the whole thing makes any difference.
>
> Just looking at the assembly output in godbolt suggest they
> create same instructions but I wonder if it makes any
> difference at compilation/linking etc.
>
Hello everyone, I had the opportunity to try the difference
between C and D within the subject. Here are code and the results:
```d
//import core.stdc.stdio : printArray = puts; /* version1
static import core.stdc.stdio;
alias printArray = core.stdc.stdio.puts; //* version2 */
void main() {
char[4] arr = ['4', '2', '\n', '\0'];
arr.ptr.printArray;
}
```
Both versions gave the same result...
**Command:** ldc2 staticImport.d -release -m64 -O
**Size:** 8.232 bytes
In DMD, the size have grown as the runtime is involved!
**Command:** dmd staticImport.d -release -m64 -O
**Size:** 897.968 bytes
---
```c
#include <stdio.h>
void main() {
char arr[4] = {'4', '2', '\n', '\0'};
printf("%s", &arr);
}
```
When we converted this simple code to C and used the DMD compiler
again, it fell in size for some reason.
**Command:** dmd staticImport.c -release -m64 -O
**Size:** 776.720 bytes
By the way, when we removed the static statement, I did not
observe a compilation error or a different result. In summary, I
like to use alias, especially in one line with import.
SDB at 79
More information about the Digitalmars-d
mailing list