Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:01:54+00:00 2026-06-10T13:01:54+00:00

I have a 5 functions defined in a class. In another class I have

  • 0

I have a 5 functions defined in a class. In another class I have a multiple looped structure that tries computing the time taken my each of the aforementioned functions on a input file containing many strings.

I want the output such that for each entry of the input file, the average time taken by each function over 10 calls is computed and printed out. So, the output should be like –

inputString | function1AvgTime | f2AvgTime and so on.

I cannot figure out an elegant way to call all these 5 functions 10 times each for a single input string. Right now what i seem to be doing is this –

    for (inputString):
         for (iteration 1 to 10):
             call function 1
         for (iteration 1 to 10):
             call function2
    and so on....

Is there a way to store the function names in an array or some datastructure, and call them within a single iteration loop? My work is getting done anyway. I’m just curious if there is a better design.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:01:56+00:00Added an answer on June 10, 2026 at 1:01 pm

    Use the Runnable interface,

    void runMultipleTimes(Runnable run, int times) {
        for (int i = 0; i < times; i++) {
            run.run();
        }
    }
    

    Then use anonymous local classes to wrap the individual functions, i.e.

    runMultipleTimes(new Runnable() {
        @Override
        void run() {
            function1();
        }
    }, 10 /* how often to run */);
    

    This is somewhat in line with the Java API. Runnable is a key interface used by Java all over the place. At the same time, it usually is only executed once, I believe. So it’s up to you to ensure it can be run multiple times!

    But so far, you have gained not much over the explicit for loop, except the ability to store the Runnables in a collection:

    for (Runnable run : runCollection) {
        runMultipleTimes(run, 100);
    }
    

    If you want to avoid writing the Runnables yourself, your can use Java reflection to get the methods by String name, or even via full introspection. Essentially, you’d then write one Runnable that executes one java.lang.reflect.Method. The Runnable approach is a bit more flexible, because you can also do things such as:

    runCollection.add(new Runnable() {
        @Override
        void run() {
            function(123456);
        }
    });
    
    runCollection.add(new Runnable() {
        @Override
        void run() {
            function(654321);
        }
    });
    

    To run the same functions with different parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with defined functions that need to be passed as parameters.
I keep getting errors that my functions have been defined multiple times. Of course
I have a functional AdjacencyListGraph class that adheres to a defined interface GraphStructure. In
I have defined a C#-class, that shall be the elements of a directed graph
I have a class Message that has a std::string as a data member, defined
I have a database that holds common functions that I use across multiple databases.
I have a simple core class that is being used for core functions to
I have a function defined in a superclass, InteractorStyle. Then I have another class
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.