WET Dilutes Performance Bottlenecks
WET Dilutes Performance Bottlenecks
Kirk Pepperdine
THE IMPORTANCE OF THE DRY PRINCIPLE (Don’t Repeat Yourself) is that it codifies the idea that every piece of knowledge in a system should have a singular representation. In other words, knowledge should be contained in a single implementation. The antithesis of DRY is WET (Write Every Time). Our code is WET when knowledge is codified in several different implemen- tations. The performance implications of DRY versus WET become very clear when you consider their numerous effects on a performance profile.
Let’s start by considering a feature of our system, say X, that is a CPU bottle- neck. Let’s say feature X consumes 30% of the CPU. Now let’s say that feature X has 10 different implementations. On average, each implementation will consume 3% of the CPU. As this level of CPU utilization isn’t worth worrying about if we are looking for a quick win, it is likely that we’d miss that this fea- ture is our bottleneck. However, let’s say that we somehow recognized feature X as a bottleneck. We are now left with the problem of finding and fixing every single implementation. With WET, we have 10 different implementations that we need to find and fix. With DRY, we would clearly see the 30% CPU utiliza- tion and would have a tenth of the code to fix. And did I mention that we don’t have to spend time hunting down each implementation?
There is one use case where we are often guilty of violating DRY: our use of collections. A common technique to implement a query would be to iterate over the collection and then apply the query in turn to each element:
public class UsageExample {
private ArrayList<Customer> allCustomers = new ArrayList<Customer>();
// ...
public ArrayList<Customer> findCustomersThatSpendAtLeast(Money amount) {
ArrayList<Customer> customersOfInterest = new ArrayList<Customer>();
for (Customer customer: allCustomers) {
if (customer.spendsAtLeast(amount))

customersOfInterest.add(customer);
}
return customersOfInterest;
}
}
By exposing this raw collection to clients, we have violated encapsulation. This not only limits our ability to refactor, but it also forces users of our code to vio- late DRY by having each of them reimplement potentially the same query. This situation can easily be avoided by removing the exposed raw collections from the API. In this example, we can introduce a new, domain-specific collective type called CustomerList. This new class is more semantically in line with our domain. It will act as a natural home for all our queries.
Having this new collection type will also allow us to easily see if these queries are a performance bottleneck. By incorporating the queries into the class, we eliminate the need to expose representation choices, such as ArrayList, to our clients. This gives us the freedom to alter these implementations without fear of violating client contracts:
public class CustomerList {
private ArrayList<Customer> customers = new ArrayList<Customer>();
private SortedList<Customer> customersSortedBySpendingLevel =
new SortedList<Customer)();
// ...
public CustomerList findCustomersThatSpendAtLeast(Money amount) {
return new CustomerList( customersSortedBySpendingLevel.elementsLargerThan(amount));
} }
public class UsageExample {
public static void main(String[] args) {
CustomerList customers = new CustomerList();
// ...
CustomerList customersOfInterest =
// ... }
}
customers.findCustomersThatSpendAtLeast(someMinimalAmount);
In this example, adherence to DRY allowed us to introduce an alternate index- ing scheme with SortedList keyed on our customers’ level of spending. More important than the specific details of this particular example, following DRY helped us to find and repair a performance bottleneck that would have been more difficult to find had the code been WET.
WET Dilutes Performance Bottlenecks的更多相关文章
- Thinking Clearly about Performance
http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...
- 怎么监视跟踪一个进程(Process)中的MS Unit Test DLL的详细性能(performance)【asp.net C#】
Sample This tutorial will show how to instrument a unit test DLL for performance profiling. Visual S ...
- Sponsored Feature: Common Performance Issues in Game Programming
转自http://www.gamasutra.com/view/feature/132084/sponsored_feature_common_.php?print=1 By Becky Heinem ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- VBA 获取Sheet最大行
compared all possibilities with a long test sheet: 0,140625 sec for lastrow = calcws.Cells.Find(&quo ...
- 【译】About the Java Technology
About the Java Technology Java technology is both a programming language and a platform. The Java Pr ...
- Percona 开始尝试基于Ceph做上层感知的分布式 MySQL 集群,使用 Ceph 提供的快照,备份和 HA 功能来解决分布式数据库的底层存储问题
本文由 Ceph 中国社区 -QiYu 翻译 英文出处:Using Ceph with MySQL 欢迎加入CCTG Over the last year, the Ceph world drew m ...
- sql是如何执行一个查询的!
引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQ ...
- 转:45 个 LoadRunner 面试问题(附答案)_纯英文,太有逼格了
What is load testing? - Load testing is to test that if the application works fine with the loads th ...
随机推荐
- C#-逆变 协变 反射 代码
首先看一段测试代码,自己写的 class Program { static void Main(string[] args) { man OneMan = new man(); var d = One ...
- 洛谷——P1352 没有上司的舞会
https://www.luogu.org/problem/show?pid=1352#sub 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树, ...
- HDU 4332 Contest 4
顶好的一道题.其实,是POJ 2411的升级版.但POJ 2411我用的插头DP来做,一时没想到那道题怎么用状态DP,于是回头看POJ 2411那一道的状态DP,其实也很简单,就是每一行都设一个状态, ...
- USACO runaround
/* ID:kevin_s1 PROG:runround LANG:C++ */ #include <iostream> #include <cstdio> #include ...
- oracle 10g/11g 命令对照,日志文件夹对照
oracle 10g/11g 命令对照,日志文件夹对照 oracle 11g 中不再建议使用的命令 Deprecated Command Replacement Commands crs_st ...
- the process android.process.acore has stopped或the process com.phone。。。。
模拟器一启动 The process android.process.acore has stopped unexpectedly 今天不知道怎么回事,模拟器一启动就狂报错, 模拟器已经重新安装过了, ...
- CoreData 从入门到精通(三)关联表的创建
上篇博客中讲了 CoreData 里增删改查的使用,学到这里已经可以应对简单的数据存储需求了.但是当数据模型复杂起来时,例如你的模型类中除了要存储 CoreData 里支持的数据类型外,还有一些自定义 ...
- 英语影视台词---八、the shawshank redemption
英语影视台词---八.the shawshank redemption 一.总结 一句话总结:肖申克的救赎 1.It's funny. On the outside, I was an honest ...
- Java中异常处理之try和catch代码块的使用
转自:https://www.jb51.net/article/72901.htm Java try和catch的使用 尽管由Java运行时系统提供的默认异常处理程序对于调试是很有用的,但通常你希望自 ...
- JS数组去重 包含去除多个 NaN
Array.prototype.uniq = function () { var arr = []; var flag = true; this.forEach(function(item) { ...