I had an argument today with one of my teacher where he was saying CGI was always slower than Servlet. I told him that performance was subjective and in some situation CGI could perform better than Servlet. He insisted on getting example of when CGI could be faster than Servlet. I just want to know what would be the most solid thing I could come up with to counter the "Servlet is always faster than CGI".
-
You really like argumentation. Your teacher is probably right in the essence. Nonetheless, I should give you credit for being cautious about the word "always". – gawi Oct 01 '10 at 18:21
-
If you take "CGI" to mean "run a [Perl|Python|whatever] script" then he's right: the servlet will typically be done before the interpreter even gets started. If you take it to mean "run a binary" then you can surely find examples like Charlie Martin's below, where a pre-compiled piece of C will load and run really quickly, quite possibly faster than the servlet. But you're splitting hairs by that point. Still, your teacher should know better than to use the word "always" ;) – Rodney Gitzel Oct 01 '10 at 20:23
-
This topic may help to understand CGI better: [I never really understood: what is CGI?](http://stackoverflow.com/questions/2089271/i-never-really-understood-what-is-cgi) – BalusC Oct 02 '10 at 03:38
2 Answers
6 years later.... Perhaps you can email your teacher this:
"On average, the PHP version is faster than the ASP version, while the CGI (C++) version is more than 10 times faster than both PHP and ASP." (http://www.wrensoft.com/zoom/benchmarks.html)
Basically, CGI performance will vary based on the language you implement it and I doubt that a servlet will run faster than a C++ CGI doing the same thing. Even more under stress, when resource consumption play a big role.
Ask your teacher why Facebook compiles PHP into a C++ equivalent, or why Google and Amazon uses C++ on their implementations.
People talk about fork() exec() overhead but don't acknowledge that JVM does the same thing internally. By the way, JVM is written in C/C++.

- 231
- 4
- 12
Performance is not subjective. Go look up the word.
Now, to answer your question, a CGI will be faster than a servlet when the time to execute the Java code of the servlet takes long enough that the time to load and execute the CGI program is dominated by the time the Java program runs. So, for example, if you had a CGI program in C that had
main(){ return; }
and you compared that with a servlet, it might well be faster.
What you want to do is set up a servlet container and a CGI directory and actually monitor and measure some comparitive performance.

- 110,348
- 25
- 193
- 263
-
I doubt that even this would be faster. You need to fork() and exec(). Compare this to processing a request on a thread that is most likely already spawn. Maybe FastCGI could win in some cases but not plain-old CGI. – gawi Oct 01 '10 at 18:23
-
It really depends on the setup. As you say, fork/exec might make the difference, but then you might also be running fastCGI so the fork./exec time is eliminated. Going the other direction, if the JVM has to swap anything significant to handle the request, that could be much more time than fork/exec. The point is that **performance is not subjective**. If you want to know which is faster, *measure*. – Charlie Martin Oct 04 '10 at 16:05