2

I am taking part in a tournament that requires the submission of a single python file. The problem requires quite a bit of computation in very little time and any performance gain would be very beneficial.

What I am wondering is, can I utilize C in some way? I know about Cython as well as C extensions but they require more than one file which means they are unusable. Is there a way to execute compiled C from inside python without another file?

8
  • You may want to be careful about doing that, you're likely to give yourself a number of problems. For this competition, do you even know the architecture this would be run on? I'd expect that they'd expect pure Python. Commented Mar 29, 2021 at 17:33
  • 1
    Does this answer your question? Can we use C code in Python? Commented Mar 29, 2021 at 17:34
  • I see a way to do it but it would be quite time consuming, are you sure you want to go down this road? @WilfredAlmeida it is not a duplicate, he wants to it in a single file. Commented Mar 29, 2021 at 17:34
  • @RunnersNum45 - without another file - that means you're not allowed to use any import, right? Commented Mar 29, 2021 at 17:38
  • Are you allowed to import libraries? @RunnersNum45 Commented Mar 29, 2021 at 17:39

1 Answer 1

1

You could achieve this in two folds, by first making your own package offering wrappers to your C code, and then publishing it with PyPi:

  • You first write a Python interface in C, I guess you already know how to do it.

  • You can follow this tutorial to publish your package, and then call your package with pip install your-package-name

Keep in mind that this process is quite lengthy, if you are in the middle of a competition I am not sure it would be the best solution, but I guess that if you ask this question you have already done all what you can do to optimize the algorithm part. You could also use one of the high performance libs that exist (numpy, pandas). If you are not even allowed to use pip then there is no way to do it in a single file.

NB: I assume that you are allowed to install packages through the terminal

Sign up to request clarification or add additional context in comments.

2 Comments

This might be my best solution. I will try a test and if it works I'll mark this as the answer.
Good luck with the implementation, let me know if it worked! Would also be interested to see the source code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.