Matlab MEX 速度减慢,无需更改代码

发布于 2024-11-05 11:39:17 字数 3939 浏览 0 评论 0原文

过去几周我一直在开发一些代码。该代码封装在 MATLAB 中的 MEX 文件中。直到昨天,我通常的测试只花了大约 1 秒。突然,在不更改任何内容的情况下重新编译代码后,相同的代码花费了 4 秒多的时间。

唯一的变化发生在与我正在调用的函数完全无关的函数中。似乎所有编译器优化都从一秒到另一秒消失了。

也许你们中的某个人过去经历过类似的事件并且知道如何处理?

感谢您的建议。

编辑1:

我认为它依赖于编译器,这里是编译命令的输出(使用详细模式)

----------------------------------------------------------------
->    MATLAB                = /Applications/MATLAB_R2010b.app
->    CC                    = gcc-4.0
->    CC flags:
         CFLAGS             = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -fexceptions
         CDEBUGFLAGS        = -g
         COPTIMFLAGS        = -O2 -DNDEBUG
         CLIBS              = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    CXX                   = g++-4.0
->    CXX flags:
         CXXFLAGS           = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
         CXXDEBUGFLAGS      = -g
         CXXOPTIMFLAGS      = -O2 -DNDEBUG
         CXXLIBS            = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    FC                    = gfortran
->    FC flags:
         FFLAGS             = -fexceptions -m64 -fbackslash
         FDEBUGFLAGS        = -g
         FOPTIMFLAGS        = -O
         FLIBS              = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
         arguments          =  -DMX_COMPAT_32
->    LD                    = gcc-4.0
->    Link flags:
         LDFLAGS            = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map
         LDDEBUGFLAGS       = -g
         LDOPTIMFLAGS       = -O
         LDEXTENSION        = .mexmaci64
         arguments          =  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib
->    LDCXX                 = 
->    Link flags:
         LDCXXFLAGS         = 
         LDCXXDEBUGFLAGS    = 
         LDCXXOPTIMFLAGS    = 
         LDCXXEXTENSION     = 
         arguments          =  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib
----------------------------------------------------------------

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file1.cpp"

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file2.cpp"

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file3.cpp"

-> gcc-4.0 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map -o  "distanceSplinePoint.mexmaci64"  file1.o file2.o file3.o  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

I have been developing a bit of code for the last few weeks. The Code is wrapped within a MEX file in MATLAB. Until yesterday my usual test took about 1 second. Suddenly after recompiling the code without changing anything, the same code takes more than 4 seconds.

The only changes happened in a function totally unrelated to the function I am calling. It seems as all compiler optimization has vanished from one second to the other.

Maybe one of you experienced similar happenings in the past and knows how to deal with that?

Thank you for your suggestions.

Edit 1:

As I assume it is something compiler dependent, here the output of the compilation command (using verbose mode)

----------------------------------------------------------------
->    MATLAB                = /Applications/MATLAB_R2010b.app
->    CC                    = gcc-4.0
->    CC flags:
         CFLAGS             = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -fexceptions
         CDEBUGFLAGS        = -g
         COPTIMFLAGS        = -O2 -DNDEBUG
         CLIBS              = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    CXX                   = g++-4.0
->    CXX flags:
         CXXFLAGS           = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
         CXXDEBUGFLAGS      = -g
         CXXOPTIMFLAGS      = -O2 -DNDEBUG
         CXXLIBS            = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    FC                    = gfortran
->    FC flags:
         FFLAGS             = -fexceptions -m64 -fbackslash
         FDEBUGFLAGS        = -g
         FOPTIMFLAGS        = -O
         FLIBS              = -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
         arguments          =  -DMX_COMPAT_32
->    LD                    = gcc-4.0
->    Link flags:
         LDFLAGS            = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map
         LDDEBUGFLAGS       = -g
         LDOPTIMFLAGS       = -O
         LDEXTENSION        = .mexmaci64
         arguments          =  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib
->    LDCXX                 = 
->    Link flags:
         LDCXXFLAGS         = 
         LDCXXDEBUGFLAGS    = 
         LDCXXOPTIMFLAGS    = 
         LDCXXEXTENSION     = 
         arguments          =  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib
----------------------------------------------------------------

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file1.cpp"

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file2.cpp"

-> g++-4.0 -c  -I/usr/include -I/Applications/MATLAB_R2010b.app/extern/include -I/Applications/MATLAB_R2010b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "file3.cpp"

-> gcc-4.0 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2010b.app/extern/lib/maci64/mexFunction.map -o  "distanceSplinePoint.mexmaci64"  file1.o file2.o file3.o  /usr/lib/libarmadillo.dylib /usr/lib/liblapack.dylib /usr/lib/libblas.dylib -L/Applications/MATLAB_R2010b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-11-12 11:39:17

有时提问就是这样:你可能会花几个小时寻找答案,但一旦你决定向某人寻求帮助,你就会奇迹般地在脑海中得到答案。

清理 MATLAB 工作区解决了我所有的问题...

不过,感谢您的阅读;)

As it sometimes is with asking questions: You can spend hours searching for the answer, but once you decided to ask somebody for help you miraculously get the answer in your head.

Clearing the MATLAB-Workspace solved all my problems...

Still, thanks for reading ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文