用于查找程序平均运行时间的脚本

发布于 2024-11-07 02:49:36 字数 777 浏览 0 评论 0原文

我在几个网站上找到了部分解决方案,所以我将几个部分放在一起,但我仍然无法弄清楚。

这就是我正在做的事情:

我正在从终端运行一个简单的 java 程序,并且需要找到该程序的平均运行时间。

我正在做的是多次运行该命令,找到总时间,然后将该总时间除以我运行该程序的次数。

我还想获取程序的输出,而不是将其显示在标准输出上。

这是我当前的代码和输出。

Shell 脚本:

   startTime=$(date +%s%N)
   for ((i = 0; i <  $runTimes; i++))
   do
            java Program test.txt > /dev/null
   done
   endTime=$(date +%s%N)
   timeDiff=$(( $endTime - $startTime ))
   timeAvg=$(( $timeDiff / $numTimes ))
   echo "Avg Time Taken: "
   echo $timeAvg

输出:

 ./run: line 12: 1305249784N: value too great for base (error token is "1305249784N")

第 12 行已关闭,因为此代码是较大文件的一部分。 第 12 行是正在评估 timeDiff 的行。

我感谢任何帮助,如果这个问题多余或偏离主题,我深表歉意。

I found partial solutions on several sites, so I pulled several parts together, but I still couldn't figure it out.

Here is what I am doing:

I am running a simple java program from Terminal, and need to find the average runtime for the program.

What I am doing is running the command several times, finding the total time, and then dividing that total time by the number of times I ran the program.

I would also like to acquire the output of the program rather than displaying it on standard output.

Here is my current code and the output.

Shell Script:

   startTime=$(date +%s%N)
   for ((i = 0; i <  $runTimes; i++))
   do
            java Program test.txt > /dev/null
   done
   endTime=$(date +%s%N)
   timeDiff=$(( $endTime - $startTime ))
   timeAvg=$(( $timeDiff / $numTimes ))
   echo "Avg Time Taken: "
   echo $timeAvg

Output:

 ./run: line 12: 1305249784N: value too great for base (error token is "1305249784N")

The line number 12 is off because this code is part of a larger file.
The line number 12 is the line with timeDiff being evaluated.

I appreciate any help, and apologize if this question is redundant or off-topic.

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

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

发布评论

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

评论(1

醉生梦死 2024-11-14 02:49:36

在我的机器上,我看不到日期的 %N 格式会给您带来什么,因为该值似乎是 7 个零,但它在数学中计算出一个更大的数字,即1305250833570000000。您真的需要纳秒级精度吗?我敢打赌,如果您只使用 %s 就可以了。

否则你看起来就走在正确的轨道上了。

PS

哦,是的,小问题,

 echo "Avg Time Taken:  $timeAvg" 

是实现所需输出的更简单方法;-)

选项 2。您可以将日期计算全部取出,并将循环变成一个小脚本。然后您可以使用 shell 的内置功能

time myJavaTest.sh

会给您详细信息,

real    0m0.049s
user    0m0.016s
sys     0m0.015s

希望这对您有所帮助。

On my machine, I don't see what the %N format for date is getting you, as the value seems to be 7 zeros, BUT it is making a much bigger number to evaluate in the math, i.e. 1305250833570000000. Do you really need nano-second precision? I'll bet if you go with just %s it will be fine.

Otherwise you look to be on the right track.

P.S.

Oh yeah, minor point,

 echo "Avg Time Taken:  $timeAvg" 

Is a a simpler way to achieve your required output ;-)

Option 2. You could take out the date calculations all together, and turn your loop into a small script. Then you can use a built-in feature of the shell

time myJavaTest.sh

Will give you details like

real    0m0.049s
user    0m0.016s
sys     0m0.015s

I hope this helps.

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