When should code favour optimization over readability and ease-of-use?

I am in the process of designing a small library, where one of my design goals is that the API should be as close to the domain language as possible. While working on the design, I’ve noticed that there are some cases in the code where a more intuitive, readable attribute/method call requires some functionally unnecessary encapsulation. Since the final product will not necessarily require high performance, I am unconcerned about making the decision to favour ease-of-use in my current project over the most efficient implementation of the code in question.

I know not to assume readability and ease-of-use are paramount in all expected use-cases, such as when performance is required. I would like to know if there are more general reasons that argue for a design preferring more efficient implementations—even if only marginally so?

4

To answer the title question: When you really need the performance gain. Real-time systems, embedded systems, or something like this. Or when the code is at the very low level and there are abstraction over abstraction layers built over it. Even then I’d prefer proven bottlenecks before optimization. You won’t win much when the code is called once in an hour. When it is called hundred thousands of times a second, then there’s a number.

Since the final product will not necessarily require high
performance…

There. Seems like you should go for readability and ease-of-use first and put a comment there: “Optimize by doing Foo. Maybe. If necessary. Better not.”

API == interface

This should be as simple and intuitive as it can be. It is also totally independent of the implementation.

Implementation == inner working

Make a clean implementation, with well structured code and good design decisions. Premature optimization is the root of evil. Take a profiler and check where you bottlenecks are.

1

When you’re building a library, or any tool that will be used by many people for different purposes, this is a problem, because they all have different needs.

What I try to do is get some representative use cases, and make sure my product makes a reasonable choice for those cases,
and is not atrociously bad in any case.

If I can just give one example of what can happen:

The LAPACK routine DGEMM is a general routine for multiplying matrices.
Its calling sequence contains initial character arguments for specifying certain options, like whether either argument is to be transposed.

The customizing arguments are in there for two purposes: to make programming easier for the user, and to make it easier for the library writer.
Without them, either the user would have to transpose the arguments him/her-self, or the library writer would have to provide multiple routines.

To handle those arguments, DGEMM calls a function LSAME that compares characters. That also makes life easier for the library writer, because character representation can be very different for some machines.

The downside of this is, if the matrices are not very large, that DGEMM spends most of its time calling LSAME, compared to the time it spends multiplying matrices!
If the user’s program spends a large fraction of time calling DGEMM, that means a large fraction of time is spent calling LSAME, comparing characters (even though those characters are practically always the same).
Repeated effort is wasted effort.
If a user can figure that out, such as by random pausing, they can make ad-hoc routines for multiplying their matrices.

But the more general point is –
that is the problem in writing libraries or any tools.
You have to try to be useful for a spectrum of needs.

0

Never optimize unless you have very good reasons. There is so much going under the hood (there is a ton of obscure optimizations in the compiler or even in the CPU itself) that it’s very hard to know what kind of impact your optimization will have. You could make it faster or slower, you will never know unless you profile it.

Also, if your application is already fast enough, why should you bother making it faster? It’s time you will waste when you could spend it making your code more readable.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật