Блог пользователя PoomrokC

Автор PoomrokC, 8 лет назад, По-английски

Hello everybody , today I'll be testing speed of input/output on Codeforces. My objective is to provide assistance on determining input method for problems (You wouldn't want to get TLE because of I/O right?) and calculate the actual time your algorithm need to work in.

(I am currently Updating this post, if you wanted your language and method to be listed here , please contact me and your name will be largely on this post Xp)!!

Since the post has become very long now , I suggest searching for specific language or you might get dizzy and I cannot deliver you medicine on time :(

Languages tested: C/C++ , JAVA

How do I test?

I made two problems in polygon , then I create mashup contest from them. So I am able to test my solutions separately .

Links to my polygon problem is as follow , you can create your own mashup and test it yourself.

If you do not wish to do that you can see screenshots of my problems that contain every information you need.

Here

test constraints are in the images above , please see before proceeding.

I do not consider 0ms and 15ms to be so much difference on codeforces , so both of them wil be considered 0ms.

INPUT

Every of the solution will follow this idea: we iterate through every input and add up into 64-bit integer then after that we mod it by 1,000,000,007

C/C++

1.Standard scanf

Code

(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 155ms #7 764ms #8 1512ms

(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 78ms #6 156ms #7 764ms #8 1528ms

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 93ms #6 156ms #7 764ms #8 1497ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 156ms #7 764ms #8 1513ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 171ms #7 779ms #8 1544ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 93ms #6 171ms #7 717ms #8 1419ms

Seems like scanf works the same regardless of compliers.

2.Standard cin

Code

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 171ms #5 810ms #6 1622ms #7 8876ms #8 >15000ms(TLE)

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 171ms #5 826ms #6 1637ms #7 8112ms #8 >15000ms(TLE)

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 140ms #5 655ms #6 1294ms #7 6239ms #8 12619ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 78ms #5 343ms #6 655ms #7 3369ms #8 6723ms

LOOK AT THIS!! FOR INTEGERS USING MVC++ 2010 COMPLIER IS FASTER THAN OTHERS

3.cin with ios_base::sync_with_stdio(false); and cin.tie(NULL);

Code

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 61ms #5 249ms #6 499ms #7 2432ms #8 4773ms

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 249ms #6 483ms #7 2432ms #8 4835ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 109ms #6 218ms #7 1091ms #8 2167ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 327ms #6 639ms #7 3135ms #8 6255ms

LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS and MVC++ 2010 had no differences compare to standard cin

4.Using getchar() , modified code from geek4geeks.org

Code

This will work for both positive and negative 32-bit integers , so you might want to try it out.

(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 30ms #6 46ms #7 140ms #8 295ms

(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 30ms #7 139ms #8 264ms

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 31ms #7 140ms #8 280ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 30ms #7 139ms #8 280ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 140ms #6 296ms #7 1403ms #8 2761ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 109ms #6 218ms #7 1122ms #8 2261ms

Something is definitely wrong with G++14,MVC++ 2010 complier with getchar() , so you want to avoid these compilers if you use this fastscan method :) (Try it for yourself , this is amazing!!)

5.Fast and Furious custom by Al.Cash

Please refer to this blog: http://codeforces.net/blog/entry/45835

And my main() here:

Code

Requires C++11 and does not work on MVC++

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 0ms #7 139ms #8 280ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 31ms #7 139ms #8 280ms

JAVA (Tested by bhishma)

1.BufferedReader and String.spit

Code

(JAVA 8) Time used on tests : #1 77ms #2 77ms #3 93ms #4 140ms #5 296ms #6 452ms #7 1653ms #8 3119ms

2.BufferedReader and StringTokenizer

Code

(JAVA 8) Time used on tests : #1 93ms #2 109ms #3 78ms #4 108ms #5 155ms #6 264ms #7 873ms #8 1606ms

3.Custom InputReader

Code

(JAVA 8) Time used on tests : #1 93ms #2 93ms #3 78ms #4 108ms #5 93ms #6 139ms #7 280ms #8 452ms

OUTPUT

C/C++

1.Standard printf

Code

(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 202ms #6 1138ms #7 2292ms

(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 46ms #4 421ms #5 4367ms #6 >15000ms(TLE) #7 >=15000ms(TLE)

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 217ms #6 1169ms #7 2324ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 218ms #6 1169ms #7 2308ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 202ms #6 1154ms #7 2262ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 233ms #6 1232ms #7 2386ms

AVOID GCC C11 if possible!!

2.Standard cout

Code

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 514ms #6 2464ms #7 4944ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 514ms #6 2433ms #7 4913ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 187ms #6 888ms #7 1684ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 77ms #5 577ms #6 2916ms #7 5834ms

LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS , EVEN PRINTF ITSELF?

3.cout with ios_base::sync_with_stdio(false);

Code

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 452ms #6 2293ms #7 4742ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 468ms #6 2292ms #7 4616ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 139ms #6 717ms #7 1403ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 697ms #6 2932ms #7 5943ms

LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS , EVEN PRINTF ITSELF? Also MVC++ 2010 does not seems to profit compare to standard cout

4.Using putchar()

Code

My code only work for positive integers!!

(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 327ms #7 686ms

(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 61ms #6 342ms #7 655ms

(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 373ms #7 686ms

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 46ms #6 327ms #7 670ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 373ms #6 2058ms #7 4102ms

(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 296ms #6 1715ms #7 3213ms

So G++14 and MVC++ 2010 also have problem with putchar too :(. Anyway , I encouraged you to modify my code to be able to use with negative integer as well :)

5.Fast and Furious custom by Al.Cash

Please refer to this blog: http://codeforces.net/blog/entry/45835

And my main() here:

Code

Requires C++11 and does not work on MVC++

(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 296ms #7 577ms

(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 61ms #6 296ms #7 592ms

JAVA (Tested by bhishma)

1.System.out.print()

Code

(JAVA 8) Time used on tests : #1 109ms #2 139ms #3 186ms #4 592ms #5 4945ms #6 >15000ms(TLE) #7 >15000ms(TLE)

2.PrintWriter

Code

(JAVA 8) Time used on tests : #1 93ms #2 124ms #3 139ms #4 140ms #5 296ms #6 904ms #7 1590ms

3.System.out.print() with StringBuilder

Code

(JAVA 8) Time used on tests : #1 108ms #2 93ms #3 109ms #4 124ms #5 249ms #6 810ms #7 1497ms

4.System.out.print() with StringBuilder but clears the StringBuilder buffer after certain writes

Code

(JAVA 8) Time used on tests : #1 124ms #2 124ms #3 124ms #4 124ms #5 264ms #6 763ms #7 1418ms

5.PrintWriter+StringBuilder

Code

(JAVA 8) Time used on tests : #1 93ms #2 78ms #3 108ms #4 108ms #5 233ms #6 779ms #7 1482ms

UPD1: Added putchar()/getchar() method for C/C++

UPD2: Added tests for MVC++ 2010 compiler

UPD3: Added JAVA tests , thanks to bhishma

UPD4: Added fast and furious C/C++ tests , thanks to Al.Cash

Полный текст и комментарии »

  • Проголосовать: нравится
  • +82
  • Проголосовать: не нравится

Автор PoomrokC, история, 8 лет назад, По-английски

Hi everyone , this is my first blog entry and it is about how to connect to Mysql with C++ language.

Motivation of writing this guide

Last three days are like nightmares to me , I try to install Mysql Connector/C++ on my Ubuntu14.04 server. Guides on Official Mysql site is somewhat very confusing and I think some part is wrong. So I'd like to write this guide to whoever finds it difficult to install it and maybe for my future use in case I mess up my own server!!

1.Installing LAMP stack if you don't already have one.

You should already have mysql installed if you read this guide , but in case you don't , Follow a nice guide here. You can choose to only install mysql , but somehow I feel safer to install all of them.

2.Install the Mysql Connector/C++

As I already say , You can follow the guide on Mysql Official site but some commands on that are wrong , so I finally choose to trust apt-get,so run this in TERMINAL:

sudo apt-get update

sudo apt-get install libmysqlcppconn-dev

If this complete successfully you should have the connector library installed.

Compiling and testing standalone program

Consider the following example code which only connect to the database.

Example Code

Just don't forget to change your host,user,pass,database Or you will get Runtime error. Save this code as tester.cpp.

We will compile this code using

sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

Which make executable name 'testapp'.

For the sake of simplicity , I created this bash script to make compiling easier:

Bash Script

and save this as cp.sh , so next time you want to compile 'tester.cpp' in to executable 'testapp' just use

./cp.sh tester.cpp testapp

Then run your very first program by running

./testapp

If your code perform successfully , nothing should be printed. If there are errors(exeption), just read and try to deal with it.

Summary

I'm not a professional so if there are mistakes I'm so sorry for it. I just want to help people that have the same problem using C++ with Mysql. For more example about the connector usage, I'm afraid you must find it somewhere else because I'm also studying and may not be able to provide you with the correct information.

Thanks everybody for reading my post!

Happy Coding!!

PoomrokC

Полный текст и комментарии »

  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится