Yes, generating the exception is more expensive than applying the regular
expression.  However, the code isn't run that often (relatively) and the
results are cached, so it shouldn't make a significant difference.

Just to verify the numbers, I put together a small test.  I ran 100000
iterations of integer, float and string values through the two methods:

+-----------------------------------+
| milliseconds to execute iteration |
+-----------------------------------+
| value type |  regex   | exception |
+------------|----------|-----------+
| integer    |  0.0484  | 0.0094    |
| float      |  0.1641  | 0.311     |
| string     |  0.1125  | 0.4796    |
+------------+----------+-----------+

The timing values were calculated by taking the total time and dividing by
the number of iterations.  The only case where using the exception method is
better is if the values are integers (in which case, no exception is thrown
since integer is the first test).

Profiling the program confirms the results (java -Xrunhprof:cpu=samples):

CPU SAMPLES BEGIN (total = 299) Fri Mar 05 08:18:27 2004
rank   self  accum   count trace method
   1 40.80% 40.80%     122    37 java.lang.Throwable.fillInStackTrace
   2  8.03% 48.83%      24    50 java.lang.StringBuffer.<init>
   3  7.02% 55.85%      21    51 java.lang.StringBuffer.expandCapacity
   4  3.01% 58.86%       9    49 java.lang.StringBuffer.expandCapacity
   5  2.68% 61.54%       8    41
java.lang.NumberFormatException.forInputString
   6  2.34% 63.88%       7    31 java.util.regex.Pattern.matcher
...

But, if this is the only code depending on java 1.4, it seems like it would
be better to remove it for better version compatibility.  Perhaps what would
be best would be to have the code detect which version it's running under
and act appropriately. 

Tim


> From: Mario Ivankovits [mailto:[EMAIL PROTECTED]
> 
> I dont know where and how often this piece of code gets 
> called and how 
> often a wrong value will be passed, but to throw an exception 
> might be 
> more expensive.
> Think of the stacktrace which needs to be filled.
> 
> Maybe the type of the term should then be cached (per field).
> 
> 
> Mario

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to