104 points · by TomasSedovic · 16y ago · 95 comments
C++ is actually a moderately effective functional programming language. With reasonable knowledge of the standard data structures, and using BOOST, one can actually write fairly expressive and effective C++ code.
Most of the time, I feel like all I'm doing is writing a much more verbose version of Python. The rest of the time, I'm hunting nightmarish segfaults and segfault and template errors.
For me, someone who learned C++ in school but has been in managed enviornments ever since, I'd be too scared to use C++. I'm not ashamed to admit I get a lot of help from the Internet when I'm stuck on a problem and that help is available because everyone else is using the same tools I am. Using C++ to code web apps puts you out in the wilderness as far as I'm concerned and I just don't think I could do it.
Speed of execution depends more on the programmer's expertise than the language used.
I'm now used to first class functions, currying, etc. C++ actually has them, hidden in the stl and boost::function. Whatever I can't do using those, I can usually do by creating a family of classes which only implement operator() (I've only needed to do this once or twice, and it might have been avoidable).
Due to using a language with native dicts (and syntactic sugar), they are now part of my vocabulary. This means I immediately reach for std::map or boost::unordered_map when it makes sense.
Similarly, python generators and haskell lazy lists have made view sequences as reasonable objects to generate and iterate over. Custom iterators are just the same thing with added verbosity.
If I never used higher level languages, I'd still be treating C++ as C with objects.
xformerlist& lbrace = node.children.front().xformations;
xformerlist& rbrace = node.children.back().xformations;
fn_and<shared_variable> row_and_flat(&shared_variable::is_row, &shared_variable::is_flat);
const sharedset& flat_ins = filter(row_and_flat, set_union_all(in, inout));
const sharedset& flat_outs = filter(row_and_flat, set_union_all(out, inout));
const sharedset& flat_all = set_union_all(flat_ins, flat_outs);
make_conditions<gen_in<row_access> > make_gen_in_row(parcond, conds, local_depths);
make_conditions<gen_out<row_access> > make_gen_out_row(parcond, conds, local_depths);
append(lbrace, fmap(make_gen_in_row, flat_ins));
append(rbrace, fmap(make_gen_out_row, flat_outs));
I imagine that code would give a lot of people fits. In my current project, I ran with a more functional approach to C++. I don't know if I'll keep everything I've used in this programming style, but I certainly will keep some of it.One nice thing is; in a fast language, tests run quickly too.
Also, Python and Ruby don't have the equivalent of an ASSERT (you could roll your own but it's not standard practice).
Also, C++ has standard hash lists and complicated data structure are actually going to run quickly.
And while memory management can be a pain, you are doing it yourself so you can track down memory leaks rather than having them be inherent in the interpreter...
...as in Ruby AND Mono.
Ahem:
http://docs.python.org/reference/simple_stmts.html#the-asser...
I think a safe assumption is that tr1::unordered_{map,set} will be available in all implementations by now, but until C++0x is ratified and implemented by major compilers, you will run into different platforms having (possibly) different implementations. And lets be honest - even after C++0x has been implemented in major compilers, you will still have subtly different/buggy implementations.
edit: I had conflated {hash,unordered}_{set,map}. Fixed
And anyway, the part that really need to be optimized can still be done in C even if you use python.
There was a time I was a C++ guy who wanted to control memory and everything.. but now, I've got other things to do. If I can write 1 line that is more readable and cost less to type, why should I use C++ ?
And by the way, C++ isn't a verbose python. And, even if I once thought that boost was the best thing ever made, I feel that it's a waste of time. Instead of using meta-programming hacks to use lambda in a clumpsy/ugly way, why not simply use python or scheme ?
This article definitely is sarcastic.
Oh joy!
AFAIK it's GPLv2 (and not AGPL) so this is only true if you intend to distribute your application itself, not just host it yourself.
Personally i thought that with better resources management i could do much more, so i wrote a custom HTTP server in that could fit in less than one MB of RAM. Most of my pages were generated offline using a custom program in FreePascal.
The server could also execute CGI scripts, so i also wrote a forum in FreePascal.
According to my logs, the whole system ran out of memory only once :-). Until the day i decided to give myself a little more features (when i got a much better VPS from Linode) i had about 5-6 sites running (different domains), a Subversion server and a few "dynamic" apps.
The forum can be found here. I still run it in my new VPS, although it got some spam. The a + b = ? anti-spam feature was new when i wrote the forum but seems that bots got better :-P.
However this does not imply a web app written in C++ will run even 1% faster than a web app written in python unless the performance bottleneck is code execution.
If the performance bottleneck is instead the database server (which it almost always is) then choosing C++ for your next webapp would be a _very_ masochistic premature optimization.
At our peak late in 2002, we served about 320 million dynamic webpages a month using three desktop Pentium 3's for webservers, and a dual CPU P3 for the database. No caching, as that wasn't needed.
Blazing fast, and not all that difficult to work with once the basic framework was solid and in place.
Also, if you want extreme scalability, like being able to serve 10000 requests/sec on a single server ... sorry, but raw performance doesn't cut it ... see this article for instance ... http://www.kegel.com/c10k.html.
Not to mention that the most usual bottleneck is the database (how many apps can you build that doesn't use one?). So even if you build the fastest web server in the world, if you're using a RDBMS you're going to end up with 100 reqs/sec, unless you're sharding or caching that data.
The bottom line is ... if you want extreme scalability, I don't think C++ is going to cut it, and you're going to invest a whole lot more in optimizations that are already done in more mature web frameworks.
Well, unless you have Google's resources and skill.
lol.
1 wchar_t *
2 utf2wide(const char *utf)
3 {
4 size_t len;
5 wchar_t *wide;
6 len = mbstowcs(NULL, utf, 0);
7 wide = malloc(sizeof(*wide) * (len + 1));
8 mbstowcs(wide, utf, len);
9 return wide;
10 }And if your implementation on the server side is very fast, you can do more.
The few times I've seen actual web stacks done in C++, the end result has been... comedic.
I'm not sure if this falls under the robust category or.. sever :) but it supports forth and has total of 360 computers running at 700 Mips or 250 Gips
Web apps in Objective C --- the second-least safe programming language
on the market. Oh please, oh please, build your next huge application
in this. College tuition for my kids is freaking me out.I still use it. Every day.
It really isn't as terrible as you'd think.
I gave a tongue-in-cheek talk on how C++ can fit in to a web application.
And, like I said, I missed that part.
It was not proper markup ;-)