Redian新闻
>
这两个程序哪个更快?
avatar
这两个程序哪个更快?# Java - 爪哇娇娃
P*e
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
avatar
xt
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.
avatar
P*e
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.

avatar
a*a
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.

avatar
xt
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();

avatar
a*a
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.

avatar
xt
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.

avatar
m*t
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?
: 两个程序:
: 一:

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。