From 02ef2c352d991891067c88861f3674b1675df7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sun, 2 Dec 2012 12:33:05 +0200 Subject: [PATCH] #304 improved Normalization performance --- .../com/mysema/query/support/Normalization.java | 17 +++++++++++++++-- .../mysema/query/support/NormalizationTest.java | 10 ++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/querydsl-core/src/main/java/com/mysema/query/support/Normalization.java b/querydsl-core/src/main/java/com/mysema/query/support/Normalization.java index c8586d3ea..bc403f35d 100644 --- a/querydsl-core/src/main/java/com/mysema/query/support/Normalization.java +++ b/querydsl-core/src/main/java/com/mysema/query/support/Normalization.java @@ -25,7 +25,11 @@ public final class Normalization { private static final Pattern MULTIPLICATION = Pattern.compile(START + NUMBER + WS + "\\*" + WS + NUMBER); - public static final String normalize(String queryString) { + public static final String normalize(String queryString) { + if (!hasOperators(queryString)) { + return queryString; + } + StringBuilder rv = null; Matcher m = OPERATION.matcher(queryString); int end = 0; @@ -74,8 +78,17 @@ public final class Normalization { } } else { return queryString; + } + } + + private static final boolean hasOperators(String queryString) { + for (int i = 0; i < queryString.length(); i++) { + char ch = queryString.charAt(i); + if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { + return true; + } } - + return false; } private Normalization(){} diff --git a/querydsl-core/src/test/java/com/mysema/query/support/NormalizationTest.java b/querydsl-core/src/test/java/com/mysema/query/support/NormalizationTest.java index 637da31d9..aa6429e73 100644 --- a/querydsl-core/src/test/java/com/mysema/query/support/NormalizationTest.java +++ b/querydsl-core/src/test/java/com/mysema/query/support/NormalizationTest.java @@ -5,6 +5,16 @@ import static org.junit.Assert.*; import org.junit.Test; public class NormalizationTest { + + @Test + public void Performance() { + int iterations = 1000000; + long start = System.currentTimeMillis(); + for (int i = 0; i < iterations; i++) { + Normalization.normalize("select name from companies where id = ?"); + } + System.err.println(System.currentTimeMillis() - start); + } @Test public void Variables() {