0

What is the equivalent of this code to java?

I want to compute for the running time of this code using java but I have no idea how. Please help me out. TIA! #include<stdio.h> #include<time.h> int main(){ clock_t begin = clock(); int i; int n=50; int sum = 0; for( i = 0; i < n; ++i ) for( j = 0; j < n * n; ++j ) ++sum; clock_t end = clock(); double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("%f\n", time_spent); }

29th Oct 2021, 2:58 PM
zenitsu
10 Antworten
+ 3
import java.util.concurrent.TimeUnit; public class Program { public static void main(String[] args) { long startTime = System.nanoTime(); int n = 50; int sum = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n * n; j++) { sum++; } } long endTime = System.nanoTime(); long duration = (endTime - startTime); long millis = TimeUnit.NANOSECONDS. toMillis(duration); System.out.println(millis); } }
29th Oct 2021, 5:50 PM
Coding Cat
Coding Cat - avatar
+ 3
Converting this code to java code
29th Oct 2021, 4:00 PM
Альфья
Альфья - avatar
+ 1
Coding Cat thanks a lot!
30th Oct 2021, 9:44 AM
zenitsu
0
Are you talking about the time complexity
29th Oct 2021, 3:57 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
♨️♨️ uhm, no. I'm talking about how to find the actual running time of a program through implementing the code
29th Oct 2021, 4:03 PM
zenitsu
0
Jhamm yes. what's the equivalent of this code to java?
29th Oct 2021, 4:03 PM
zenitsu
0
You are wanting this same code written in Java so you can compare the two CLOCKS_PER_Second to each other?
29th Oct 2021, 4:45 PM
William Owens
William Owens - avatar
0
William Owens yeees that's what I want to know
29th Oct 2021, 5:26 PM
zenitsu
- 1
Jhamm what did u do? did u just add package and class? that's not what im asking but thanks!
29th Oct 2021, 4:24 PM
zenitsu
- 1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Popup Animation</title> </head> <body> <div class="loader"></div> <a class="link-btn" href="https://youtube.com/channel/UCyozn5waSjZ4hr0_uFQk8WQ"><span class="link">learn to code </span></a> </body> </html>
31st Oct 2021, 11:54 AM
فيديوهات متنوعه