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 ...
随机推荐
- vue解决跨域问题
vue解决跨域问题 vue跨域解决方法和小总结 vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow ...
- 使用 Xshell 连接 linux 系统
一.下载 Xshell 链接:https://pan.baidu.com/s/1htwqpzm 密码:zau7 二.安装 Xshell 无脑下一步就可以了 三.连接 linux 四.安装 Xftp h ...
- ZOJ 3688
做出这题,小有成就感 本来已打算要用那个禁位的排列公式,可是,问题在于,每个阶乘前的系数r的求法是一个难点. 随便翻了翻那本美国教材<组合数学>,在容斥原理一章的习题里竟有一道类似,虽然并 ...
- Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...
- 低效能的”where1=1”
网上有不少人提出过类似的问题:"看到有人写了where 1=1这种sql,究竟是什么意思?".事实上使用这种使用方法的开发者一般都是在使用动态数组的sql. 让我们想象例如以下的场 ...
- android 退出系统
/** * */ package com.szkingdom.android.phone.utils; import java.io.BufferedReader; import java.io.IO ...
- php设计模式之桥接模式
php设计模式之桥接模式 一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 其实就是讲不相关的东西通过类(本例中是SendInfo)结合在一起,从而 ...
- (五)api网关服务 zuul-路由
路由是微服务架构中必须的一部分,比如,“/” 可能映射到你的WEB程序上,”/api/users “可能映射到你的用户服务上,“/api/shop”可能映射到你的商品服务商.(注解:我理解这里的这几个 ...
- .net core 实现npoi导出
Nuget 安装 NPOI 2.4.1 工作时间写着测试玩玩 public void ExportDataToExcel() { var workbook = new HSSFWorkbook(); ...
- linux ps 命令查看进程状态
显示其他用户启动的进程(a) 查看系统中属于自己的进程(x) 启动这个进程的用户和它启动的时间(u) 使用“date -s”命令来修改系统时间 比如将系统时间设定成1996年6月10日的命令如下. # ...