TL;DR: The difference is noticeable only if you are working with a big strings (millions of bytes/characters).
All tests were compiled and executed on iMac 21.5 Late 2012, 2.7GHz Intel Core i5.
I've made a small benchmark. Here is the code.
interpolation.swift compiled with swiftc ./interpolation.swift -o ./interpolation
import Swift
_ = "\(Process.arguments[1]) \(Process.arguments[2])"
Output of swiftc
with -emit-assembly
flag:
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.globl _main
.align 4, 0x90
_main:
.cfi_startproc
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
subq $128, %rsp
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_token5@GOTPCREL(%rip), %rax
movq __TZvOs7Process5_argcVs5Int32@GOTPCREL(%rip), %rcx
movl %edi, (%rcx)
cmpq $-1, (%rax)
movq %rsi, -56(%rbp)
je LBB0_2
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_token5@GOTPCREL(%rip), %rdi
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_func5@GOTPCREL(%rip), %rax
movq %rax, %rsi
callq _swift_once
LBB0_2:
movl $5, %eax
movl %eax, %edi
movq __TZvOs7Process11_unsafeArgvGSpGSpVs4Int8__@GOTPCREL(%rip), %rcx
movq -56(%rbp), %rdx
movq %rdx, (%rcx)
callq __TTSg5SS___TFs27_allocateUninitializedArrayurFBwTGSax_Bp_
leaq L___unnamed_1(%rip), %rdi
xorl %esi, %esi
movl $1, %r8d
movq %rdx, -64(%rbp)
movl %r8d, %edx
movq %rax, -72(%rbp)
callq __TFSSCfT21_builtinStringLiteralBp8byteSizeBw7isASCIIBi1__SS
movq %rax, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq __TFSSCfT26stringInterpolationSegmentSS_SS
movq -64(%rbp), %rsi
movq %rax, (%rsi)
movq %rdx, 8(%rsi)
movq %rcx, 16(%rsi)
callq __TFOs7Processau9argumentsGSaSS_
movq (%rax), %rax
movq %rax, %rdi
movq %rax, -80(%rbp)
callq _swift_bridgeObjectRetain
leaq -24(%rbp), %rdi
movl $1, %r8d
movl %r8d, %esi
movq -80(%rbp), %rdx
movq %rax, -88(%rbp)
callq __TTSg5SS___TFSag9subscriptFSix
movq -80(%rbp), %rdi
callq _swift_bridgeObjectRelease
movq -24(%rbp), %rdi
movq -16(%rbp), %rsi
movq -8(%rbp), %rdx
callq __TFSSCfT26stringInterpolationSegmentSS_SS
leaq L___unnamed_2(%rip), %rdi
movl $1, %r8d
movl %r8d, %esi
movl $1, %r8d
movq -64(%rbp), %r9
movq %rax, 24(%r9)
movq %rdx, 32(%r9)
movq %rcx, 40(%r9)
movl %r8d, %edx
callq __TFSSCfT21_builtinStringLiteralBp8byteSizeBw7isASCIIBi1__SS
movq %rax, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq __TFSSCfT26stringInterpolationSegmentSS_SS
movq -64(%rbp), %rsi
movq %rax, 48(%rsi)
movq %rdx, 56(%rsi)
movq %rcx, 64(%rsi)
callq __TFOs7Processau9argumentsGSaSS_
movq (%rax), %rax
movq %rax, %rdi
movq %rax, -96(%rbp)
callq _swift_bridgeObjectRetain
leaq -48(%rbp), %rdi
movl $2, %r8d
movl %r8d, %esi
movq -96(%rbp), %rdx
movq %rax, -104(%rbp)
callq __TTSg5SS___TFSag9subscriptFSix
movq -96(%rbp), %rdi
callq _swift_bridgeObjectRelease
movq -48(%rbp), %rdi
movq -40(%rbp), %rsi
movq -32(%rbp), %rdx
callq __TFSSCfT26stringInterpolationSegmentSS_SS
leaq L___unnamed_1(%rip), %rdi
xorl %r8d, %r8d
movl %r8d, %esi
movl $1, %r8d
movq -64(%rbp), %r9
movq %rax, 72(%r9)
movq %rdx, 80(%r9)
movq %rcx, 88(%r9)
movl %r8d, %edx
callq __TFSSCfT21_builtinStringLiteralBp8byteSizeBw7isASCIIBi1__SS
movq %rax, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq __TFSSCfT26stringInterpolationSegmentSS_SS
movq -64(%rbp), %rsi
movq %rax, 96(%rsi)
movq %rdx, 104(%rsi)
movq %rcx, 112(%rsi)
movq -72(%rbp), %rdi
callq __TFSSCft19stringInterpolationGSaSS__SS
movq %rcx, %rdi
movq %rax, -112(%rbp)
movq %rdx, -120(%rbp)
callq _swift_unknownRelease
xorl %eax, %eax
addq $128, %rsp
popq %rbp
retq
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.space 1
L___unnamed_2:
.asciz " "
.linker_option "-lswiftCore"
.linker_option "-lobjc"
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 768
.subsections_via_symbols
addstr.swift (+
operator) compiled with swiftc ./addstr.swift -o ./addstr
import Swift
_ = Process.arguments[1] + " " + Process.arguments[2]
Output of swiftc
with -emit-assembly
flag:
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 9
.globl _main
.align 4, 0x90
_main:
.cfi_startproc
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
subq $176, %rsp
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_token5@GOTPCREL(%rip), %rax
movq __TZvOs7Process5_argcVs5Int32@GOTPCREL(%rip), %rcx
movl %edi, (%rcx)
cmpq $-1, (%rax)
movq %rsi, -56(%rbp)
je LBB0_2
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_token5@GOTPCREL(%rip), %rdi
movq _globalinit_33_1BDF70FFC18749BAB495A73B459ED2F0_func5@GOTPCREL(%rip), %rax
movq %rax, %rsi
callq _swift_once
LBB0_2:
movq __TZvOs7Process11_unsafeArgvGSpGSpVs4Int8__@GOTPCREL(%rip), %rax
movq -56(%rbp), %rcx
movq %rcx, (%rax)
callq __TFOs7Processau9argumentsGSaSS_
movq (%rax), %rax
movq %rax, %rdi
movq %rax, -64(%rbp)
callq _swift_bridgeObjectRetain
leaq -24(%rbp), %rdi
movl $1, %edx
movl %edx, %esi
movq -64(%rbp), %rdx
movq %rax, -72(%rbp)
callq __TTSg5SS___TFSag9subscriptFSix
movq -64(%rbp), %rdi
callq _swift_bridgeObjectRelease
leaq L___unnamed_1(%rip), %rdi
movl $1, %r8d
movl %r8d, %esi
movl $1, %edx
movq -24(%rbp), %rax
movq -16(%rbp), %rcx
movq -8(%rbp), %r9
movq %r9, -80(%rbp)
movq %rcx, -88(%rbp)
movq %rax, -96(%rbp)
callq __TFSSCfT21_builtinStringLiteralBp8byteSizeBw7isASCIIBi1__SS
movq -96(%rbp), %rdi
movq -88(%rbp), %rsi
movq -80(%rbp), %r9
movq %rdx, -104(%rbp)
movq %r9, %rdx
movq %rcx, -112(%rbp)
movq %rax, %rcx
movq -104(%rbp), %r8
movq -112(%rbp), %r9
callq __TZFsoi1pFTSSSS_SS
movq %rax, -120(%rbp)
movq %rdx, -128(%rbp)
movq %rcx, -136(%rbp)
callq __TFOs7Processau9argumentsGSaSS_
movq (%rax), %rax
movq %rax, %rdi
movq %rax, -144(%rbp)
callq _swift_bridgeObjectRetain
leaq -48(%rbp), %rdi
movl $2, %r10d
movl %r10d, %esi
movq -144(%rbp), %rdx
movq %rax, -152(%rbp)
callq __TTSg5SS___TFSag9subscriptFSix
movq -144(%rbp), %rdi
callq _swift_bridgeObjectRelease
movq -48(%rbp), %rcx
movq -40(%rbp), %r8
movq -32(%rbp), %r9
movq -120(%rbp), %rdi
movq -128(%rbp), %rsi
movq -136(%rbp), %rdx
callq __TZFsoi1pFTSSSS_SS
movq %rcx, %rdi
movq %rax, -160(%rbp)
movq %rdx, -168(%rbp)
callq _swift_unknownRelease
xorl %eax, %eax
addq $176, %rsp
popq %rbp
retq
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L___unnamed_1:
.asciz " "
.linker_option "-lswiftCore"
.linker_option "-lobjc"
.section __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
.long 0
.long 768
.subsections_via_symbols
As you can see, the assembly of addstr.swift contains less commands than interpolation.swift.
Here are the benchmark results using /usr/bin/time
for timing (bash-3.2).
$ ARG1=$(printf '%.0s' {1..30000}) # 30000 '' characters
$ ARG2=$(printf '%.0s' {1..30000}) # 30000 '' characters
$ time ./interpolation $ARG1 $ARG2
>
> real 0m0.026s
> user 0m0.018s
> sys 0m0.006s
$ time ./addstr $ARG1 $ARG2
>
> real 0m0.026s
> user 0m0.018s
> sys 0m0.006s
I've run this test lots of times, but the results were always the same (±0.001s).