1

I recently moved into web development using ASP.NET MVC. The language I use is C#. Having considerable experience in C makes me look for optimized coding standards (memory, efficient data structures and speed of the loops to name a few).

But my peers who have years of experience in this domain do not seem to take into account all these. So my question is, does the code need to be optimized when working with such languages as the servers we will be deploying these applications on has huge memory and processing speeds? Or is ignoring them a bad practice?

gvk
  • 335
  • 1
  • 8
  • 1
    Are you under the impression the software written by your peers has severe performance problems, and the general speed could be improved by a factor >100 by some optimizing? Or are you just wondering why your peers don't micro-optimize software which is already fast enough for the given requirements? – Doc Brown Sep 27 '15 at 07:38
  • Well, I cannot say the factor by which it improves the performance. But the code I saw had around 15 global variables (all objects), in a class where each object had minimal to no use in some methods – gvk Sep 27 '15 at 07:54
  • 1
    And what performance problem do you believe these 15 global (presumably you mean static?) variables will cause? Other than a trivial memory overhead (120 bytes for the references, and probably between 2 and 20 times that for the objects they refer to) keeping the objects permanently rather than allocating and destroying them when they are needed presumably makes the methods that do use them faster. Memory is cheap, so why the concern over such a small use of it? – Jules Sep 27 '15 at 11:46
  • @Jules No, they were instance variables – gvk Sep 27 '15 at 13:26
  • 1
    Performance problems are *never* where you think they are. I only know of one absolutely sure-fire can't-miss way to find them, and that is to use a small number of [*random stack samples*](http://stackoverflow.com/a/378024/23771). I work in a large C# project, and it has found some *large* speedups. The point is, assuming there is a real performance problem, no matter what it is, fixing it will save some fraction of time, like from 10% to 90%. The probability you will catch it in the very act on any single sample is at least that large. (It is extremely unlikely to be what you're guessing.) – Mike Dunlavey Sep 29 '15 at 00:13

2 Answers2

6

The culture of programming has changed considerably since the times that C was the language of choice and the hardware was so wimpy that C was actually a necessity.

Luckily, we do not have to worry about optimization so much nowadays. The general rule is to never optimize unless you have a good reason to do so. And in order to have a good reason, you have to:

  1. Have a pre-established performance requirement for your product, something like "server response time must be less than 200 milliseconds". (Vague requirements like "as fast as possible" are generally frowned upon.)

  2. Measure the actual response time of your server and actually witness that it is failing to meet the requirement.

  3. Exhaust all options of meeting the requirement by reconfiguring your system to make it work more optimally. (You would be surprised. Some people don't know the difference between running a web server in debug mode and in production mode.)

  4. Exhaust all options of meeting the requirement by buying better hardware and/or more hardware. Hardware nowadays generally costs far less than developers' salaries.

  5. Throw the profiler on your system and determine that the bottleneck is in fact in code that you are responsible for and have the power to change.

  6. Exhaust all options for algorithmic optimizations. (Introduction of a caching layer somewhere; restructuring code so that something happens asynchronously rather than synchronously; etc.)

Then, and only then, is it advisable to try your luck by tweaking code to make it work more optimally. And as you might imagine, we hardly ever reach this stage.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111
3

In most business application development the focus is on delivering functionality and writing code as cleanly and maintainable as possible in order to avoid bugs and make it easier to react to business changes and to deliver new functionality in the future. Performance optimizations usually have a cost in development time and in more complex code, which is then harder to modify later. Therefore optimizations can be counterproductive.

The usual rule of thumb is to only optimize if there is an actual observable performance problem, rather than optimize "by default" as is more common in systems development.

JacquesB
  • 57,310
  • 21
  • 127
  • 176