首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将一段代码循环x次并取平均值

如何将一段代码循环x次并取平均值
EN

Stack Overflow用户
提问于 2013-04-14 06:46:05
回答 2查看 89关注 0票数 0

我希望循环下面代码的主要部分,然后计算从循环中收集的结果的平均值

代码如下:

代码语言:javascript
复制
class PingPongVariousLengths {

    static public void main(String[] args) {

        MPI.Init(args);
        int myrank = MPI.COMM_WORLD.Rank();
        int tag = 99;
        int maxlen = 104857600; //200 megabytes     104857600 characters * 2 bytes per character = 209715200 bytes total, or 200 megabytes
        int minlen = 1; // 2 bytes
        char [] sendbuff = new char [maxlen];
        char [] recvbuff = new char [maxlen];
        long speedKbps;
        long speedMbps;


        if (myrank == 0) {
            for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time
                long startTime = System.nanoTime();                  
                 MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag);
                 MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag);
                long endTime = System.nanoTime();
                long duration = endTime - startTime;
                long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds
                System.out.println("Time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds"); // multiples by 2 becuase 1 character is 2 bytes
                //double transferRate = ((len*2.0) / durationseconds ) ; //amount of data in bytes transferred in 1 second. Currently returning 0 for every result
                //System.out.println("transferRate: " + transferRate + " bytes per second");
                double transferRateMb = ((len*524288.0) / durationseconds ) ; //amount of data in megabytes transferred in 1 second. 
                System.out.println("transferRate (megabytes) : " + transferRateMb + " megabytes per second");
                }
        } else if (myrank == 1) {
            for (int len = minlen; len <= maxlen; len *= 2) {
                 MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag);
                 MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag);
            }
        }


        MPI.Finalize();
    }
}

我尝试将它循环10或20次,代码来自

代码语言:javascript
复制
            for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time

代码语言:javascript
复制
                long durationseconds = (duration * 10-9); // Converts nanoseconds to seconds

我想运行20次,然后duration的结果平均超过10或20

我怎样才能最好地、最有效率地去做这件事呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-14 07:10:08

如果我没理解错的话,您希望每个长度增量的平均持续时间为秒。为了获得持续时间秒的平均值,您需要将长度循环移到平均循环之外。请参阅下面的代码片段。

代码语言:javascript
复制
long durationseconds;
int MAX_LOOPS = 20;

for (int len = minlen; len <= maxlen; len *= 2) {
        if (myrank == 0) {
                durationseconds = 0;
                for (int i = 0; i < MAX_LOOPS; i++) {
                        long startTime = System.nanoTime();           
                        MPI.COMM_WORLD.Send(sendbuff, 0, len, MPI.CHAR, 1, tag);
                        MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 1, tag);
                        long endTime = System.nanoTime();
                        long duration = endTime - startTime;
                        durationseconds = durationseconds + (duration * 10-9);
                }
                durationseconds = durationseconds / MAX_LOOPS;
                System.out.println("Average time for the ping to be sent and recived of " + (len*2) + " bytes is " + durationseconds + " seconds");
                double transferRateMb = ((len*524288.0) / durationseconds );
                System.out.println("average transferRate (megabytes) : " + transferRateMb + " megabytes per second");
        } else if (myrank == 1) {
                MPI.COMM_WORLD.Recv(recvbuff, 0, len, MPI.CHAR, 0, tag);
                MPI.COMM_WORLD.Send(recvbuff, 0, len, MPI.CHAR, 0, tag);
        }
}
票数 1
EN

Stack Overflow用户

发布于 2013-04-14 06:51:21

代码语言:javascript
复制
long totalDuration = 0;
for (int i = 0; i<nTimes;i++){
... //Code N stuff here
totalDuration += duration;
}
long avgDuration = totalTime /nTimes;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15993769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档