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 ...
随机推荐
- 洛谷 1156 dp
洛谷1156 dp 类背包问题 老久没有自己想出来过dp方程了,,,虽然到最后还是只写了30分,,, 设dp[j]表示最大生命值为i时的最大高度,则对于每个物品,可以选择吃掉或者放上去,即转移为dp[ ...
- ofbiz初级教程
本教程是ofbiz 基本应用,它涵盖了OFBiz应用程序开发过程的基本原理.目标是使开发人员熟悉最佳实践,编码惯例,基本控制流程以及开发人员对OFBiz定制所需的所有其他方面. 本教程将帮助您在OFB ...
- cocos2d-x3.2 下使用多线程
事实上在cocos2dx下使用多线程事实上就是用C++去写,这里提供几个简单的样例: 原文地址:http://blog.csdn.net/qqmcy/article/details/36227377 ...
- HDU 4415 Assassin's Creed(贪心)
pid=4415">HDU 4415 题意: 壮哉我Assassin! E叔有一柄耐久度为m的袖剑,以及n个目标士兵要去解决. 每解决掉一个士兵,消耗袖剑Ai的耐久度.且获得该士兵的武 ...
- Linux内核中进程上下文和中断上下文的理解
參考: http://www.embedu.org/Column/Column240.htm http://www.cnblogs.com/Anker/p/3269106.html 首先明白一个概念: ...
- 为什么整个互联网行业都缺前端project师?
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- 高速排序算法C++实现
//quick sort //STL中也有现成的高速排序算法.内部实现採用了下面技巧 //1)枢轴的选择採取三数取中的方式 //2)后半段採取循环的方式实现 //3)高速排序与插入排序结合 #incl ...
- jsp的凝视可能会影响页面载入速度
在jsp页面使用"<!-- -->"的凝视,凝视里面的java代码还是会得到运行,能够再查看页面源码上看到运行完毕的内容,这样就会让不希望运行的代码得到运行.影响载入速 ...
- Eclipse中將Java项目转变为Java Web项目
1.在项目上点击右键=>properties,在Project Facets配置项中,勾选Dynamic Web Module.Java.JavaScript选项. 2.用记事本打开项目目录下的 ...
- RabbitMq笔记(2)
今天收获不少,记个笔记. namespace RabbitMQTest { class Program { static void Main(string[] args) { Consumer(); ...