在java中屏蔽信用卡号码
我尝试使用字符“X”屏蔽信用卡号码字符串中的字符。我编写了两个函数,如下所示。第二个函数使用 commons.lang.StringUtils
类。我试图找出它所花费的时间在这两种情况下
public static String maskCCNumber(String ccnum){
long starttime = System.currentTimeMillis();
int total = ccnum.length();
int startlen=4,endlen = 4;
int masklen = total-(startlen + endlen) ;
StringBuffer maskedbuf = new StringBuffer(ccnum.substring(0,startlen));
for(int i=0;i<masklen;i++) {
maskedbuf.append('X');
}
maskedbuf.append(ccnum.substring(startlen+masklen, total));
String masked = maskedbuf.toString();
long endtime = System.currentTimeMillis();
System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
System.out.println("using StringBuffer="+ (endtime-starttime)+" millis");
return masked;
}
public static String maskCCNumberCommons(String ccnum){
long starttime = System.currentTimeMillis();
int total = ccnum.length();
int startlen=4,endlen = 4;
int masklen = total-(startlen + endlen) ;
String start = ccnum.substring(0,startlen);
String end = ccnum.substring(startlen+masklen, total);
String padded = StringUtils.rightPad(start, startlen+masklen,'X');
String masked = padded.concat(end);
long endtime = System.currentTimeMillis();
System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
System.out.println("using Stringutils="+(endtime-starttime)+" millis");
return masked;
}
public static void ccNumberMaskingDemo() {
String mcard1="5555555555554444";
maskCCNumber(mcard1);
maskCCNumberCommons(mcard1);
}
,当我运行这个时,我得到了这个结果,
maskCCNumber:=5555XXXXXXXX4444 of :16 size
using StringBuffer=0 millis
maskCCNumber:=5555XXXXXXXX4444 of :16 size
using Stringutils=25 millis
我无法理解为什么commons.StringUtils比第一个函数中的for循环+StringBuffer花费更多的时间。显然我正在使用api,错误的方式..
在这种情况下,有人可以建议如何正确使用这个 api 吗?
I tried to mask the characters in a creditcard number string using character 'X'.I wrote two functions as below .The second function uses commons.lang.StringUtils
class .I tried to find the time it takes in both cases
public static String maskCCNumber(String ccnum){
long starttime = System.currentTimeMillis();
int total = ccnum.length();
int startlen=4,endlen = 4;
int masklen = total-(startlen + endlen) ;
StringBuffer maskedbuf = new StringBuffer(ccnum.substring(0,startlen));
for(int i=0;i<masklen;i++) {
maskedbuf.append('X');
}
maskedbuf.append(ccnum.substring(startlen+masklen, total));
String masked = maskedbuf.toString();
long endtime = System.currentTimeMillis();
System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
System.out.println("using StringBuffer="+ (endtime-starttime)+" millis");
return masked;
}
public static String maskCCNumberCommons(String ccnum){
long starttime = System.currentTimeMillis();
int total = ccnum.length();
int startlen=4,endlen = 4;
int masklen = total-(startlen + endlen) ;
String start = ccnum.substring(0,startlen);
String end = ccnum.substring(startlen+masklen, total);
String padded = StringUtils.rightPad(start, startlen+masklen,'X');
String masked = padded.concat(end);
long endtime = System.currentTimeMillis();
System.out.println("maskCCNumber:="+masked+" of :"+masked.length()+" size");
System.out.println("using Stringutils="+(endtime-starttime)+" millis");
return masked;
}
public static void ccNumberMaskingDemo() {
String mcard1="5555555555554444";
maskCCNumber(mcard1);
maskCCNumberCommons(mcard1);
}
When I ran this ,I got this result
maskCCNumber:=5555XXXXXXXX4444 of :16 size
using StringBuffer=0 millis
maskCCNumber:=5555XXXXXXXX4444 of :16 size
using Stringutils=25 millis
I can't understand why commons.StringUtils is taking more time than the for loop+StringBuffer in the first function.Obviously I am using the api ,the wrong way..
Can someone advise how to use this api correctly, in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
干得好。干净且可重复使用:
示例调用:
祝你好运。
Here you go. Clean and reusable:
Sample Calls:
Good luck.
使用 Apache StringUtils...
Using Apache StringUtils...
这是一个基于 StringUtils 的稍微干净的实现,尽管我不确定它与您的实现相比会如何执行。无论如何,“过早优化”的评论仍然非常有效。
Here's a slightly cleaner implementation based on StringUtils, though I am not sure how it would perform in comparison to your implementations. At any rate, the 'premature optimization' comments remain very valid.
首先,如果您对这样一个短时间运行的代码进行测量,由于您的 CPU/库/任何提供的最小定时分辨率(这意味着您通常会看到 0ms 或相同的小值),您通常无法获得准确的结果超过)。
其次,也是更重要的是,不要对此进行优化! “过早的优化是万恶之源”,如果您只有几毫秒的时间,那么您想要优化的努力就彻底浪费了。在您甚至不应该考虑优化这种简单的屏蔽方法之前,您必须屏蔽数百万张信用卡。
Firstly, if you make measurements of such a short-running code, you often do not get accurate results due to the minimal timing resolution your CPU/library/whatever provides (which means you usually get to see 0ms or the same small value over and over).
Second and more importantly, do not optimize this! "Premature optimization is the root of all evil" and in a case where you have only a few ms that you want to optimize the effort is thoroughly wasted. You would have to mask millions of credit cards before you should even remotely think about optimizing this simple mask method.
我知道这不是一个答案,但您可以使用正则表达式并一步解决这个问题
说明:
I know this is not an answer but you can use regular expression and solve this in one step
Explanation:
String utils 可能会复制字符串几次。例如,当您运行 padded.concat(end); 时jvm 分配两个连接字符串大小的新字符串并复制它们。如果您使用 StringBuffer,您将保存所有这些副本,因为缓冲区已经分配了位置,并且连接的字符串刚刚复制到那里。对我来说,StringBuffer 更快是有道理的,尽管测量的时间似乎比我预期的要长。
String utils probably copies the string few times. for example when you run padded.concat(end); the jvm allocates new string of the size of the two concat strings and copy them. If you use StringBuffer you saves all those copies as the buffer already has place allocated and the concated string just copied there. make sense to me that the StringBuffer is faster although, the time measured seems rather large then I would expect.
下面的代码将屏蔽 75% 的字符串。
Below code will mask 75% of the string.
最有可能的是,这是从
apache-commons.jar
文件加载StringUtils
的时间。不是真正的执行时间。要计算实际执行时间,请尝试运行多次,看看第二次需要多少毫秒。第 3 到第 100 名将被接受。
无论如何,正如 Frank 所说,不建议优化到这个级别。
Most probably, this is the time of
StringUtils
being loaded from theapache-commons.jar
file. Not the real execution time.To calculate the real execution time, try to run multiple times and see how much much ms will the 2nd. 3rd up to 100th will take.
Anyway, as Frank said, optimizing to this level is not recommended.
尽管可读性较差,但您可以在我的机器上使用 Google Caliper 执行此操作,
与使用 StringBuilder 或 StringUtils.overlay + 重复方法的超过 100 纳秒相比,会产生大约 20-25 纳秒。
Although less readable you can do this
Using Google Caliper on my machine would yield about 20-25 ns compared to over 100ns with StringBuilder or StringUtils.overlay + repeat approaches.
输出:XXXXXXXXXXXX6785
Out put : XXXXXXXXXXXX6785