由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 这两个程序哪个更快?
相关主题
请问StringBuffer的OutofMemory问题请教高手一个JDBC的问题!
int --> String?HELP! SQL Server vs JDBC question
a simple questionJDBC如何获取新加入的记录的索引
Java练习题 3发现weblogic 8.1 workshop一个bug
Java StringBuilder myth debunkedsigh, Java needs arraycopy for imaging
我自己编了个Java面试题问一道关于Vector的题
为什么我这个参数的内容存不下?用java访问数据库
初学者code请教 (大牛莫取笑)一个Java程序员的话(4)--续第一章
相关话题的讨论汇总
话题: string话题: fred话题: sql话题: char
进入Java版参与讨论
1 (共1页)
P****e
发帖数: 385
1
面试问题:
Often we have to send text as part of an SQL query. These need to be quoted in
single quotes, and existing single quotes need to be doubled up. E.g. Fred =>
‘Fred’, Fred’s => ‘Fred’’s’.
The following are two methods in Java (and/or VB) called QuotedString which
will take a
string as an input, and will return a quoted string appropriate for insertion
into a piece of SQL. Which one is cheaper?
两个程序:
一:
public static String QuotedString(String Original){
char[] a;
char divider = '\'';
int l
xt
发帖数: 17532
2
I guess it is the first one. If you use StringBuffer
and then insert characters, all the characters after
the insertion point have to be shifted. That means
a massive copy. Also if the length of the string is
longer than the length of StringBuffer, StringBuffer
has to append new storage space for it.
I don't really care too much about the difference
between these two examples, as long as the string is
not too long.
P****e
发帖数: 385
3
Thanks. My first impression was that operating on object is always slower than
operating on primitive data types, but I guess that's too general a thought.
Thank you for the detailed reply.
I am thinking if there is a faster approach than the first one.

【在 xt 的大作中提到】
: I guess it is the first one. If you use StringBuffer
: and then insert characters, all the characters after
: the insertion point have to be shifted. That means
: a massive copy. Also if the length of the string is
: longer than the length of StringBuffer, StringBuffer
: has to append new storage space for it.
: I don't really care too much about the difference
: between these two examples, as long as the string is
: not too long.

a*****a
发帖数: 1429
4

It is not necessary. If you claim a large capacity
StringBuffer, StringBuffer.insert is equivlant to
char array's approach, because you have to convert the
char array to a string and this actually calls
System.arrayCopy(), the same function that is called
in StringBuffer.insert();

【在 xt 的大作中提到】
: I guess it is the first one. If you use StringBuffer
: and then insert characters, all the characters after
: the insertion point have to be shifted. That means
: a massive copy. Also if the length of the string is
: longer than the length of StringBuffer, StringBuffer
: has to append new storage space for it.
: I don't really care too much about the difference
: between these two examples, as long as the string is
: not too long.

xt
发帖数: 17532
5

The problem is for char array you only need to copy once.
For StringBuffer with a lot of insertions, this is not the
case at all.
Again, I dislike using insert methods. It is ugly and should
not be encouraged.

【在 a*****a 的大作中提到】
:
: It is not necessary. If you claim a large capacity
: StringBuffer, StringBuffer.insert is equivlant to
: char array's approach, because you have to convert the
: char array to a string and this actually calls
: System.arrayCopy(), the same function that is called
: in StringBuffer.insert();

a*****a
发帖数: 1429
6

twice. insertint and converting.
Read StringBuffer's source first:-). Insert uses arraycopy
too.

【在 xt 的大作中提到】
:
: The problem is for char array you only need to copy once.
: For StringBuffer with a lot of insertions, this is not the
: case at all.
: Again, I dislike using insert methods. It is ugly and should
: not be encouraged.

xt
发帖数: 17532
7

OK, twice. For a string of size 400, if you have 40 special
characters randomly distributed over the entire array, the
difference between insert and append may not be noticeable
if you do it only once, but it is still an unnecessary
performance drain that usually can easily be avoided.
what is your point? arraycopy is not much better than using a
for-loop in terms of time complexity. OK, I let you use memcpy
in C, that still is a linear time function.

【在 a*****a 的大作中提到】
:
: twice. insertint and converting.
: Read StringBuffer's source first:-). Insert uses arraycopy
: too.

m******t
发帖数: 2416
8

[snipped]
I guess theoretically, method 1 guarantees O(n) performance, while
method 2 might be O(n^2) in the worst case (the string is full of single
quotes).
But in reality if it's a SQL, I think you should use PrepareStatement. 8-)

【在 P****e 的大作中提到】
: 面试问题:
: Often we have to send text as part of an SQL query. These need to be quoted in
: single quotes, and existing single quotes need to be doubled up. E.g. Fred =>
: ‘Fred’, Fred’s => ‘Fred’’s’.
: The following are two methods in Java (and/or VB) called QuotedString which
: will take a
: string as an input, and will return a quoted string appropriate for insertion
: into a piece of SQL. Which one is cheaper?
: 两个程序:
: 一:

1 (共1页)
进入Java版参与讨论
相关主题
一个Java程序员的话(4)--续第一章Java StringBuilder myth debunked
java问题:如何match两个正规表达式我自己编了个Java面试题
Java,EJB的performance为什么我这个参数的内容存不下?
garbage collection issue初学者code请教 (大牛莫取笑)
请问StringBuffer的OutofMemory问题请教高手一个JDBC的问题!
int --> String?HELP! SQL Server vs JDBC question
a simple questionJDBC如何获取新加入的记录的索引
Java练习题 3发现weblogic 8.1 workshop一个bug
相关话题的讨论汇总
话题: string话题: fred话题: sql话题: char