In C++, am I paying for what I am not eating?
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Let's consider the following hello world examples in C
and C++
:
main.c
#include <stdio.h>
int main()
printf("Hello worldn");
return 0;
main.cpp
#include <iostream>
int main()
std::cout<<"Hello world"<<std::endl;
return 0;
When I compile them in godbolt to assembly, the size of the C
code is only 9 lines (gcc -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edi, OFFSET FLAT:.LC0
call puts
xor eax, eax
add rsp, 8
ret
But the size of the C++
code is 22 lines (g++ -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edx, 11
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
xor eax, eax
add rsp, 8
ret
_GLOBAL__sub_I_main:
sub rsp, 8
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
add rsp, 8
jmp __cxa_atexit
which is much larger.
It is famous that in C++
you pay for what you eat. So, in this case, what am I paying for?
c++ c
New contributor
 |Â
show 9 more comments
up vote
7
down vote
favorite
Let's consider the following hello world examples in C
and C++
:
main.c
#include <stdio.h>
int main()
printf("Hello worldn");
return 0;
main.cpp
#include <iostream>
int main()
std::cout<<"Hello world"<<std::endl;
return 0;
When I compile them in godbolt to assembly, the size of the C
code is only 9 lines (gcc -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edi, OFFSET FLAT:.LC0
call puts
xor eax, eax
add rsp, 8
ret
But the size of the C++
code is 22 lines (g++ -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edx, 11
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
xor eax, eax
add rsp, 8
ret
_GLOBAL__sub_I_main:
sub rsp, 8
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
add rsp, 8
jmp __cxa_atexit
which is much larger.
It is famous that in C++
you pay for what you eat. So, in this case, what am I paying for?
c++ c
New contributor
3
You pay for initializingstd::cout
.
â Yksisarvinen
54 mins ago
2
For using iostream. In this case for std::cout.
â P.W
53 mins ago
3
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
3
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
6
You are eatingcout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.
â molbdnilo
45 mins ago
 |Â
show 9 more comments
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Let's consider the following hello world examples in C
and C++
:
main.c
#include <stdio.h>
int main()
printf("Hello worldn");
return 0;
main.cpp
#include <iostream>
int main()
std::cout<<"Hello world"<<std::endl;
return 0;
When I compile them in godbolt to assembly, the size of the C
code is only 9 lines (gcc -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edi, OFFSET FLAT:.LC0
call puts
xor eax, eax
add rsp, 8
ret
But the size of the C++
code is 22 lines (g++ -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edx, 11
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
xor eax, eax
add rsp, 8
ret
_GLOBAL__sub_I_main:
sub rsp, 8
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
add rsp, 8
jmp __cxa_atexit
which is much larger.
It is famous that in C++
you pay for what you eat. So, in this case, what am I paying for?
c++ c
New contributor
Let's consider the following hello world examples in C
and C++
:
main.c
#include <stdio.h>
int main()
printf("Hello worldn");
return 0;
main.cpp
#include <iostream>
int main()
std::cout<<"Hello world"<<std::endl;
return 0;
When I compile them in godbolt to assembly, the size of the C
code is only 9 lines (gcc -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edi, OFFSET FLAT:.LC0
call puts
xor eax, eax
add rsp, 8
ret
But the size of the C++
code is 22 lines (g++ -O3
):
.LC0:
.string "Hello world"
main:
sub rsp, 8
mov edx, 11
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
xor eax, eax
add rsp, 8
ret
_GLOBAL__sub_I_main:
sub rsp, 8
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
add rsp, 8
jmp __cxa_atexit
which is much larger.
It is famous that in C++
you pay for what you eat. So, in this case, what am I paying for?
c++ c
c++ c
New contributor
New contributor
edited 39 mins ago
Lundin
101k16147251
101k16147251
New contributor
asked 55 mins ago
Saher
391
391
New contributor
New contributor
3
You pay for initializingstd::cout
.
â Yksisarvinen
54 mins ago
2
For using iostream. In this case for std::cout.
â P.W
53 mins ago
3
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
3
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
6
You are eatingcout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.
â molbdnilo
45 mins ago
 |Â
show 9 more comments
3
You pay for initializingstd::cout
.
â Yksisarvinen
54 mins ago
2
For using iostream. In this case for std::cout.
â P.W
53 mins ago
3
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
3
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
6
You are eatingcout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.
â molbdnilo
45 mins ago
3
3
You pay for initializing
std::cout
.â Yksisarvinen
54 mins ago
You pay for initializing
std::cout
.â Yksisarvinen
54 mins ago
2
2
For using iostream. In this case for std::cout.
â P.W
53 mins ago
For using iostream. In this case for std::cout.
â P.W
53 mins ago
3
3
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
3
3
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
6
6
You are eating
cout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.â molbdnilo
45 mins ago
You are eating
cout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.â molbdnilo
45 mins ago
 |Â
show 9 more comments
2 Answers
2
active
oldest
votes
up vote
8
down vote
So, in this case, what am I paying for?
std::cout
is more powerful and complicated than printf
. It supports things like locales, stateful formatting flags, and more.
If you don't need those, use std::printf
or std::puts
- they're available in <cstdio>
.
It is famous that in C++ you pay for what you eat.
I also want to make it clear that C++ != The C++ Standard Library. The Standard Library is supposed to be general-purpose and "fast enough", but it will often be slower than a specialized implementation of what you need.
On the other hand, the C++ language strives to make it possible to write code without paying unnecessary extra hidden costs (e.g. opt-in virtual
, no garbage collection).
I think it is worth mentioning that one major advantage of streams as opposed toprintf
is that they can be taught to work with arbitrary types.
â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
@Lundin Yes, but you can't teachprintf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload ofoperator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.
â lisyarus
10 mins ago
add a comment |Â
up vote
3
down vote
The Input / Output functions in C++ are elegantly written and are designed so they are simple to use. In many respects they are a showcase for the object-orientated features in C++.
But you do indeed give up a bit of performance in return, but that's negligible compared to the time taken by your operating system to handle the functions at a lower level.
You can always fall back to the C style functions as they are part of the C++ standard, or perhaps give up portability altogether and use direct calls to your operating system.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
So, in this case, what am I paying for?
std::cout
is more powerful and complicated than printf
. It supports things like locales, stateful formatting flags, and more.
If you don't need those, use std::printf
or std::puts
- they're available in <cstdio>
.
It is famous that in C++ you pay for what you eat.
I also want to make it clear that C++ != The C++ Standard Library. The Standard Library is supposed to be general-purpose and "fast enough", but it will often be slower than a specialized implementation of what you need.
On the other hand, the C++ language strives to make it possible to write code without paying unnecessary extra hidden costs (e.g. opt-in virtual
, no garbage collection).
I think it is worth mentioning that one major advantage of streams as opposed toprintf
is that they can be taught to work with arbitrary types.
â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
@Lundin Yes, but you can't teachprintf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload ofoperator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.
â lisyarus
10 mins ago
add a comment |Â
up vote
8
down vote
So, in this case, what am I paying for?
std::cout
is more powerful and complicated than printf
. It supports things like locales, stateful formatting flags, and more.
If you don't need those, use std::printf
or std::puts
- they're available in <cstdio>
.
It is famous that in C++ you pay for what you eat.
I also want to make it clear that C++ != The C++ Standard Library. The Standard Library is supposed to be general-purpose and "fast enough", but it will often be slower than a specialized implementation of what you need.
On the other hand, the C++ language strives to make it possible to write code without paying unnecessary extra hidden costs (e.g. opt-in virtual
, no garbage collection).
I think it is worth mentioning that one major advantage of streams as opposed toprintf
is that they can be taught to work with arbitrary types.
â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
@Lundin Yes, but you can't teachprintf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload ofoperator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.
â lisyarus
10 mins ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
So, in this case, what am I paying for?
std::cout
is more powerful and complicated than printf
. It supports things like locales, stateful formatting flags, and more.
If you don't need those, use std::printf
or std::puts
- they're available in <cstdio>
.
It is famous that in C++ you pay for what you eat.
I also want to make it clear that C++ != The C++ Standard Library. The Standard Library is supposed to be general-purpose and "fast enough", but it will often be slower than a specialized implementation of what you need.
On the other hand, the C++ language strives to make it possible to write code without paying unnecessary extra hidden costs (e.g. opt-in virtual
, no garbage collection).
So, in this case, what am I paying for?
std::cout
is more powerful and complicated than printf
. It supports things like locales, stateful formatting flags, and more.
If you don't need those, use std::printf
or std::puts
- they're available in <cstdio>
.
It is famous that in C++ you pay for what you eat.
I also want to make it clear that C++ != The C++ Standard Library. The Standard Library is supposed to be general-purpose and "fast enough", but it will often be slower than a specialized implementation of what you need.
On the other hand, the C++ language strives to make it possible to write code without paying unnecessary extra hidden costs (e.g. opt-in virtual
, no garbage collection).
answered 53 mins ago
Vittorio Romeo
51.3k14137273
51.3k14137273
I think it is worth mentioning that one major advantage of streams as opposed toprintf
is that they can be taught to work with arbitrary types.
â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
@Lundin Yes, but you can't teachprintf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload ofoperator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.
â lisyarus
10 mins ago
add a comment |Â
I think it is worth mentioning that one major advantage of streams as opposed toprintf
is that they can be taught to work with arbitrary types.
â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
@Lundin Yes, but you can't teachprintf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload ofoperator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.
â lisyarus
10 mins ago
I think it is worth mentioning that one major advantage of streams as opposed to
printf
is that they can be taught to work with arbitrary types.â lisyarus
36 mins ago
I think it is worth mentioning that one major advantage of streams as opposed to
printf
is that they can be taught to work with arbitrary types.â lisyarus
36 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
@lisyarus The format string of printf is variable, so that's a weak argument. The real advantage with iostream is rather that it's somewhat robust, while stdio is a museum of the most dangerous functions ever designed in the history of programming.
â Lundin
18 mins ago
1
1
@Lundin Yes, but you can't teach
printf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload of operator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.â lisyarus
10 mins ago
@Lundin Yes, but you can't teach
printf
how to print a user-defined type instead of specifying the format every time (you can in some implementations, but, as far as I know, it is not recommended to use this feature). With streams, you can just put another overload of operator <<
without the need to repeat yourself. Of course, I agree that safety is another major advantage, in some cases the major one.â lisyarus
10 mins ago
add a comment |Â
up vote
3
down vote
The Input / Output functions in C++ are elegantly written and are designed so they are simple to use. In many respects they are a showcase for the object-orientated features in C++.
But you do indeed give up a bit of performance in return, but that's negligible compared to the time taken by your operating system to handle the functions at a lower level.
You can always fall back to the C style functions as they are part of the C++ standard, or perhaps give up portability altogether and use direct calls to your operating system.
add a comment |Â
up vote
3
down vote
The Input / Output functions in C++ are elegantly written and are designed so they are simple to use. In many respects they are a showcase for the object-orientated features in C++.
But you do indeed give up a bit of performance in return, but that's negligible compared to the time taken by your operating system to handle the functions at a lower level.
You can always fall back to the C style functions as they are part of the C++ standard, or perhaps give up portability altogether and use direct calls to your operating system.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The Input / Output functions in C++ are elegantly written and are designed so they are simple to use. In many respects they are a showcase for the object-orientated features in C++.
But you do indeed give up a bit of performance in return, but that's negligible compared to the time taken by your operating system to handle the functions at a lower level.
You can always fall back to the C style functions as they are part of the C++ standard, or perhaps give up portability altogether and use direct calls to your operating system.
The Input / Output functions in C++ are elegantly written and are designed so they are simple to use. In many respects they are a showcase for the object-orientated features in C++.
But you do indeed give up a bit of performance in return, but that's negligible compared to the time taken by your operating system to handle the functions at a lower level.
You can always fall back to the C style functions as they are part of the C++ standard, or perhaps give up portability altogether and use direct calls to your operating system.
edited 40 mins ago
answered 52 mins ago
Bathsheba
168k26238354
168k26238354
add a comment |Â
add a comment |Â
Saher is a new contributor. Be nice, and check out our Code of Conduct.
Saher is a new contributor. Be nice, and check out our Code of Conduct.
Saher is a new contributor. Be nice, and check out our Code of Conduct.
Saher is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52442415%2fin-c-am-i-paying-for-what-i-am-not-eating%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
You pay for initializing
std::cout
.â Yksisarvinen
54 mins ago
2
For using iostream. In this case for std::cout.
â P.W
53 mins ago
3
It's a myth that C++ is equally efficient as C, simple as that. Such believes only work in theory but rarely in practice. That being said, this question isn't answerable unless you post your optimization settings for each compiler.
â Lundin
50 mins ago
3
@Lundin: there's nothing preventing C++ from being as fast as C. Standard Libraries are another matter.
â Vittorio Romeo
49 mins ago
6
You are eating
cout
, so I don't see what you're paying for that you don't eat. That you can still your hunger with either lobster or an egg sandwich doesn't mean that you get to buy the lobster for the same price as an egg sandwich if your goal is just to not be hungry.â molbdnilo
45 mins ago