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); }
10 Réponses
+ 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);
}
}
+ 3
Converting this code to java code
+ 1
Coding Cat thanks a lot!
0
Are you talking about the time complexity
0
♨️♨️ uhm, no. I'm talking about how to find the actual running time of a program through implementing the code
0
Jhamm yes. what's the equivalent of this code to java?
0
You are wanting this same code written in Java so you can compare the two CLOCKS_PER_Second to each other?
0
William Owens yeees that's what I want to know
- 1
Jhamm what did u do? did u just add package and class? that's not what im asking but thanks!
- 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>