Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Java vs C++ speed (IO & Sorting)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Martin York, post: 3494858"] Just to chime in and write the C++ in a more C++ style. #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <algorithm> #include <vector> using namespace std; const int len = 50000000; struct myRandom { char operator()() const { return std::rand() % 26 + 'a';} }; int main( int argc, char *argv[]) { std::vector<char> data(len); clock_t start = clock(); std::generate_n(&data[0],len,myRandom()); std::string str(data.begin(),data.end()); clock_t end = clock(); std::cout <<"Time: " << double(end-start)/CLOCKS_PER_SEC * 1000 << " ms" << std::endl; } import java.util.*; public class Find{ public static void main(String[] arg) { final int len = 50000000; long start = System.currentTimeMillis(); String s = randomString(len); long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " ms"); } //returns a String of length l with lowercase letters from a to z static String randomString(int len) { char[] cr = new char[len]; Random rd = new Random(); for (int i = 0; i < len; i++){ int num = rd.nextInt(26) + 97; cr [i] = (char) num; } return new String(cr); } Time: 3654 ms Time: 873.487 ms[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Java vs C++ speed (IO & Sorting)
Top