-6
\$\begingroup\$

Not a new one:

Input N, being n, 0 ≤ n ≤ 5.

Output N times "Hello World."

Example:

Input

3

Output

Hello World.
Hello World.
Hello World.

These 2 are my best solutions:

First

public class Out 
{ 
    public static void main(String[] a) throws Exception 
    { 
            java.io.OutputStream o = System.out; 
            byte n = Byte.parseByte(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()); 
            for(byte i = 0;i<n;i++){
                o.write("Hola mundo.\n".getBytes());
            }
    } 
}

Second

public class Out 
{
    public static void main(String[] a) throws Exception{ 
        byte n = Byte.parseByte(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()); 
        while(n>0){switch(n){case 0: w();break;case 1: w();break;case 2: w();break;case 3: w();break;case 4: w();break;case 5: w();break;}n--;}
    }

    static void w() throws Exception{
        System.out.write("Hola mundo.\n".getBytes());
    }
}

According to the server it is being tested on ( im not an owner of that server), first attempt gives me 0.006ms and 622 KiB while second attempt gives me same time with 625 KiB.

I am not able to provide the Java version being used but if you want, feel free to use the main site ( spanish language ) https://www.aceptaelreto.com/problem/statement.php?id=116

P.S.: Some peoeple were able to get it on 0.005ms and 88KiB.

edit: -this code is being benchmarked on the link used before. -The point is to get the fastest code ( and if possible, with the least amount of memory used), not the shortest one.

Apparently Im not able to use comments since I do not have the required reputation score.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ How did you benchmark your code? \$\endgroup\$
    – stephanmg
    Nov 7, 2019 at 11:50
  • 8
    \$\begingroup\$ Is it fastest-code as the title and body suggests, or code-golf as you've tagged it? \$\endgroup\$
    – Jo King
    Nov 7, 2019 at 12:20
  • 9
    \$\begingroup\$ Do you understand that of the 6ms all but a few microseconds is the program loading overhead? \$\endgroup\$ Nov 7, 2019 at 13:20
  • \$\begingroup\$ This is a poor-quality question per @someone's comment above, and I've downvoted it, but it's not closeworthy, either as unclear or for lacking a winning criterion. \$\endgroup\$ Nov 7, 2019 at 20:08
  • \$\begingroup\$ Yeah I think it should not be closed. But just improve the question. \$\endgroup\$
    – stephanmg
    Nov 8, 2019 at 16:10

2 Answers 2

1
\$\begingroup\$

C++11 approach (Time: 3.6 µs on some Intel i7, File size of a.out: 28K) compiled via:

clang -O3 -march=native -Ofast golf.cpp: (Clang 6.1)

#include <iostream>
#include <chrono>
#include <sstream>

int main(int argc, char** argv) {
  auto t1 = std::chrono::high_resolution_clock::now();
  const int n = 5;
  if (n < 0 || n > 5) return 1;
  std::stringstream ss;
  for (size_t i = 0; i < n; i++) ss << "Hello world\n";
  std::cout << ss.str();
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  std::cout << duration << "µs" << std::endl;
  return 0;
}
\$\endgroup\$
1
  • \$\begingroup\$ One could add the check for the n you specified, but this feels boring. \$\endgroup\$
    – stephanmg
    Nov 7, 2019 at 12:03
1
\$\begingroup\$

Using C (size <12 KiB, time < 0.001s)

$ cat >hola.c <<END_C
#include<unistd.h>
int main(){
  int i,n;
  read(0,&n,1);
  for(i='0';i<n;++i){
    write(1,"Hola mundo.\n",12);
  }
}
END_C

$ gcc hola.c -o hola

$ wc -c hola
11944 hola

$ time ./hola <<<5
[...]
real    0m0.001s
user    0m0.000s
sys     0m0.000s
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.