diff --git a/openquake/baselib/performance.py b/openquake/baselib/performance.py
index <HASH>..<HASH> 100644
--- a/openquake/baselib/performance.py
+++ b/openquake/baselib/performance.py
@@ -23,6 +23,7 @@ import getpass
 import operator
 import itertools
 from datetime import datetime
+from decorator import decorator
 import psutil
 import numpy
 try:
@@ -316,6 +317,23 @@ class Monitor(object):
             return '<%s>' % msg
 
 
+def vectorize_arg(idx):
+    """
+    Vectorize a function efficientlyif the argument with index `idx` contains
+    many repetitions.
+    """
+    def caller(func, *args):
+        args = list(args)
+        uniq, inv = numpy.unique(args[idx], return_inverse=True)
+        res = []
+        for arg in uniq:
+            args[idx] = arg
+            res.append(func(*args))
+        return numpy.array(res)[inv]
+
+    return decorator(caller)
+
+
 # numba helpers
 if numba:
Added decorator vectorize_arg