22

In interpreted programming languages, such as PHP and JavaScript, what are the repercussions of going with an Object Oriented approach over a Procedural approach?

Specifically what I am looking for is a checklist of things to consider when creating a web application and choosing between Procedural and Object Oriented approaches, to optimize not only for speed, but maintainability as well. Cited research and test cases would be helpful as well if you know of any articles exploring this further.

Bottom line: how big (if any) is the performance hit really, when going with OO vs. Procedural in an interpreted language?

intboolstring
  • 6,891
  • 5
  • 30
  • 44
cmcculloh
  • 47,596
  • 40
  • 105
  • 130

7 Answers7

17

Maybe I'm crazy but worrying about speed in cases like this using an interpretive language is like trying to figure out what color to paint the shed. Let's not even get into the idea that this kind of optimization is entirely pre-mature.

You hit the nail on the head when you said 'maintainability'. I'd choose the approach that is the most productive and most maintainable. If you need speed later, it ain't gonna come from switching between procedural versus object oriented coding paradigms inside an interpreted language.

Louis Brandy
  • 19,028
  • 3
  • 38
  • 29
  • I had the boss from hell. He knew nothing about programming, but thought he knew everything (he lectured me on Unix timestamps once, telling me that, "Unix timestamps are like European timestamps. They do it weird, they put the day first and then the month like this: dd/mm/yyyy" ROFL, what an idiot). So when he found out I was doing OO PHP, he flipped out and said it would "slow down our site". I was looking for any sort of study I could find to prove he was full of it so that I could continue programming OO... – cmcculloh Aug 24 '09 at 16:47
  • OOP is the less productive and less maintainable. – Pablo Ariel Dec 18 '16 at 18:21
  • @cmcculloh Yes in Europe it's the day first to the year dd/mm/yyyy, basically the smallest up to the largest, and in China it's yyyy/mm/dd which is the largest down to the smallest. Obviously those are the only ways that logically make sense. – Hasen Mar 11 '19 at 05:33
  • Hmmm, funny analogy, it doesn't make sense to me, it can be easy to choose the color of the shed at the end, it doesn't impact the overall design of the shed. Switching a codebase between procedural and OO styles is complex, it will almost require a full rewrite of the code. – ericcurtin Mar 19 '20 at 13:52
10

Unfortunately, I've done my tests too. I did test speed, and it's about the same, but when testing for memory usage getting memory_get_usage() in PHP, I saw an overwhelmingly larger number on the OOP side.

116,576 bytes for OOP to 18,856 bytes for procedural. I know "Hardware is cheap", but come on! 1,000% increase in usage? Sorry, that's not optimal. And having so many users hitting your website at once, I'm sure your RAM would just burn, or run out. Am I wrong?

George
  • 189
  • 2
  • 2
4

Bottom line: no, because the overhead of interpretation overwhelms the overhead of method dispatching.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
2

Your performance will be characterized by the implementation, not the language. You could use the slowest language and it could scale to be the biggest site in the world as long as you design it to scale.

Just remember the first rule of optimiztion.

Don't.

:)

Jon Clegg
  • 3,870
  • 4
  • 25
  • 22
2

In my experience, a site under heavy load will be bogged down and become unresponsive much more easily with OOP code than procedural. The reason is easy to understand.

OOP requires a lot more memory allocations (MALLOC) and a lot more operations to run in memory than procedural code. It requires a lot more CPU time to perform its tasks. It is essentially 'overhead', wrapped around procedural code, adding to the CPU burden to execute it, especially when performing database operations.

Many programmers like the convenience of OOP, creating little black boxes hidden behind simple interfaces. However, I have been paid well to revive sites that were taking forever to respond under heavy user load. Stripping out the OOP and replacing it with simple procedural functions made a huge difference.

If you don't expect your site to be very busy, by all means use OOP. If you are building a high-traffic system, you'll want to strip every CPU cycle from the processing and every byte from the output that you can.

Division Six
  • 304
  • 3
  • 7
1

If you are using an interpreted language, the difference is irrelevant. You should not be using an interpreted language if performance is an issue. Both will perform about the same.

Justin Standard
  • 21,347
  • 22
  • 80
  • 89
0

I've actually done a small test like this in python on a website I maintain and found that they are almost equivalent in speed, with the procedural approach winning by something like ten-thousandths of a second, but that the OO code was so significantly cleaner I didn't continue the exercise any longer than one iteration.

So really, it doesn't matter (in my experience anyway).

dwestbrook
  • 5,108
  • 3
  • 25
  • 20