以 %.7f 格式创建时间戳字符串的更快方法

发布于 2024-10-22 09:29:03 字数 544 浏览 3 评论 0原文

这是一个我必须以 %.7f 格式创建时间戳字符串的函数。该函数执行仅需 2-3 毫秒。但它是从我的代码中的许多地方调用的,即使优化 1 毫秒,我也会在一个特定的用户操作中节省 1 秒。有什么想法吗?

public static String makeTimestamp()
{
    long millis = System.currentTimeMillis();
    String result;
    Double ts = new Double((millis) / 1000.0);

    ByteArrayOutputStream b = new ByteArrayOutputStream();
    PrintStream p = new PrintStream(b);
    p.printf("%.7f", ts );
    result = b.toString();
    try
    {
        p.close();
        b.close();
    } catch (IOException ioe) {};
    return result;
}

This is a function I have to create timestamp string in %.7f format. This function takes only 2-3 ms to execute. But its called from many places in my code, by even optimizing it by 1 ms I will save 1 second in one particular user action. Any ideas?

public static String makeTimestamp()
{
    long millis = System.currentTimeMillis();
    String result;
    Double ts = new Double((millis) / 1000.0);

    ByteArrayOutputStream b = new ByteArrayOutputStream();
    PrintStream p = new PrintStream(b);
    p.printf("%.7f", ts );
    result = b.toString();
    try
    {
        p.close();
        b.close();
    } catch (IOException ioe) {};
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2024-10-29 09:29:03

如果你确实需要它,这比字符串格式化大约快 10 倍:

public static String makeTimestamp() {
    return formatTime(System.currentTimeMillis(), 7);
}

public static String formatTime(long millis, int fractionDigits) {
    int integerDigits = (int) Math.log10(millis / 1000.0) + 1;

    char[] chars = new char[integerDigits + fractionDigits + 1];
    for (int i = 0; i < chars.length; i++) {
        chars[i] = '0';
    }

    millis *= Math.pow(10, fractionDigits - 3);
    for (int i = chars.length - 1; i >= 0; i--) {
        if (i == integerDigits) {
            chars[i] = '.';
            i--;
        }

        chars[i] = (char) (millis % 10);
        chars[i] += '0';

        millis /= 10;
    }

    return new String(chars);
}

If you absolutely need it, this is roughly 10 times faster than string formatting:

public static String makeTimestamp() {
    return formatTime(System.currentTimeMillis(), 7);
}

public static String formatTime(long millis, int fractionDigits) {
    int integerDigits = (int) Math.log10(millis / 1000.0) + 1;

    char[] chars = new char[integerDigits + fractionDigits + 1];
    for (int i = 0; i < chars.length; i++) {
        chars[i] = '0';
    }

    millis *= Math.pow(10, fractionDigits - 3);
    for (int i = chars.length - 1; i >= 0; i--) {
        if (i == integerDigits) {
            chars[i] = '.';
            i--;
        }

        chars[i] = (char) (millis % 10);
        chars[i] += '0';

        millis /= 10;
    }

    return new String(chars);
}
葬心 2024-10-29 09:29:03

我不知道它快了多少,但是怎么样

public static String makeTimestamp()
{
    return String.format("%.7f", ((double)System.currentTimeMillis())/1000);
}

(我不确定为什么我们要在图片中添加双倍)。

I don't know how much faster it is, but how about

public static String makeTimestamp()
{
    return String.format("%.7f", ((double)System.currentTimeMillis())/1000);
}

(and I am not sure why we bring double into the picture).

演多会厌 2024-10-29 09:29:03

根本不清楚为什么要经历 PrintStream 等。有什么问题:

public static String makeTimestamp()
{
    long millis = System.currentTimeMillis();
    return String.format("%.7f", millis / 1000.0);
}

在我的上网本上,每次迭代大约需要 0.04 毫秒...但即使您的原始代码每次迭代也只需要大约 0.1 毫秒。你现在正在运行什么,需要 2-3 毫秒?例如,您是否在调试器中运行?我不明白为什么它会这么慢——我的上网本并不是一台特别快的机器。

尽管所有性能都很重要,但我想说上述方法相对于原始方法的主要好处是简单。原文非常复杂,没有任何充分的理由。

It's not at all clear why you're going through a PrintStream etc. What's wrong with:

public static String makeTimestamp()
{
    long millis = System.currentTimeMillis();
    return String.format("%.7f", millis / 1000.0);
}

On my netbook that takes around 0.04ms per iteration... but even your original code only takes around 0.1ms per iteration. What are you running on that takes 2-3ms at the moment? Are you running in a debugger, for example? I can't understand why it would be so slow - it's not like my netbook is a particularly fast machine.

All performance matters aside though, I'd say the main benefit of the above method over your original is simplicity. The original is very complicated for no good reason at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文